00001 #include "sadie.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 void DISK2IMG (
00012 char *name,
00013 long size,
00014 IMAGE **out
00015
00016 ) {
00017 static char array[2] = { 0x00, 0x01 };
00018 char msg[SLEN];
00019 short nbnd, nlin, npix, nbit;
00020 long nbytes;
00021 FILE *fp=NULL;
00022 long calcsize;
00023 long readbytes;
00024
00025
00026 if (TIMES) TIMING(T_DISK2IMG);
00027
00028
00029
00030 if (!(fp=fopen(name,"r"))) {
00031 sprintf(msg," Can't open file %s.",name);
00032 MESSAGE('E',msg);
00033 goto the_end;
00034 }
00035
00036
00037
00038
00039
00040 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) {
00041 sprintf(msg," Can't read image header from file %s.",name);
00042 MESSAGE('E',msg);
00043 goto the_end;
00044 }
00045
00046
00047
00048 if (*(short *)array != 1) {
00049
00050
00051 nbnd = SWAP((unsigned char *)&nbnd);
00052 nlin = SWAP((unsigned char *)&nlin);
00053 npix = SWAP((unsigned char *)&npix);
00054 nbit = SWAP((unsigned char *)&nbit);
00055 }
00056
00057
00058 if (size > 0) {
00059 calcsize = 512L + lrnd((double)nbnd*(double)nlin*(double)npix*((double)nbit / 8.0));
00060 if (size < calcsize || calcsize <= 512L) {
00061 printf("size = %d, calcsize = %d\n",size,calcsize);
00062 printf("nbnd = %d, nlin = %d, npix = %d, nbit = %d\n",nbnd,nlin,npix,nbit);
00063 sprintf(msg,"File %s is not in SADIE format!",name);
00064 MESSAGE('E',msg);
00065 goto the_end;
00066 }
00067 }
00068
00069
00070
00071 if (!CHECKIMG(*out)) GETMEM(nbnd,nlin,npix,out);
00072 if (!CHECKIMG(*out)) {
00073 sprintf(msg,"Can't allocate memory for image.");
00074 MESSAGE('E',msg);
00075 goto the_end;
00076 }
00077
00078
00079
00080 (*out)->nbit = nbit;
00081 nbytes = (long)(sizeof((*out)->gmin)+sizeof((*out)->gmax)+sizeof((*out)->pstr)+sizeof((*out)->text));
00082 if (fread(&((*out)->gmin),nbytes,1,fp) != 1) {
00083 sprintf(msg," Can't read image header from file %s.",name);
00084 MESSAGE('E',msg);
00085 goto the_end;
00086 }
00087
00088
00089
00090
00091 if ((*out)->nbit != sizeof(PIXEL)*SBPB && (sizeof(PIXEL)*SBPB < (*out)->nbit || (*out)->nbit > sizeof(long)*SBPB)) {
00092 sprintf(msg," Can't unpack from %d bits/pixel.",(*out)->nbit);
00093 MESSAGE('E',msg);
00094 goto the_end;
00095 }
00096
00097
00098
00099
00100 nbytes = (long)ceil(((double)nbnd*(double)nlin*(double)npix*(*out)->nbit)/(double)SBPB);
00101 readbytes = fread((*out)->data[0][0],1,nbytes,fp);
00102 if (readbytes < nbytes) {
00103 sprintf(msg," Can't read image data from file %s.",name);
00104 MESSAGE('E',msg);
00105 sprintf(msg," Read %ld bytes instead of %ld bytes.",readbytes,(long)nbytes);
00106 MESSAGE('E',msg);
00107 }
00108
00109 if ((*out)->nbit < sizeof(PIXEL)*SBPB) {
00110 UNPACK((*out)->nbit,(long)nbnd*(long)nlin*(long)npix,(*out)->data[0][0]);
00111 }
00112
00113
00114 sprintf((*out)->text,"%s",name);
00115
00116
00117 if (NAMES) {
00118 MESSAGE('I',"");
00119 MESSAGE('I',"DISK2IMG");
00120 MESSAGE('I',"");
00121 sprintf(msg," Input file: %s",name);
00122 MESSAGE('I',msg);
00123 MESSAGE('I'," Input format: SADIE");
00124 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)));
00125 MESSAGE('I',msg);
00126 sprintf(msg," Number of bands, lines, pixels: %d, %d, %d",(*out)->nbnd,(*out)->nlin,(*out)->npix);
00127 MESSAGE('I',msg);
00128 sprintf(msg," Number of bits/pixel/band: %d",(*out)->nbit);
00129 MESSAGE('I',msg);
00130 sprintf(msg," Graylevel minimum, maximum: %10.4e, %10.4e",(*out)->gmin,(*out)->gmax);
00131 MESSAGE('I',msg);
00132 MESSAGE('I'," ...............");
00133 }
00134
00135
00136
00137 the_end:
00138 if (fp) {
00139 if (fclose(fp)) {
00140 sprintf(msg," Can't close file %s.",name);
00141 MESSAGE('W',msg);
00142 }
00143 }
00144
00145 if (TIMES) TIMING(T_EXIT);
00146
00147 }
00148