00001 #include <stdio.h>
00002 #include "sadie.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 void IMG2DISK (
00014 IMAGE *in,
00015 short nbit,
00016 char *name
00017
00018 ) {
00019 static char array[2] = { 0x00, 0x01 };
00020 char msg[SLEN];
00021 short nbnd, nlin, npix;
00022 long nbytes;
00023 FILE *fp=NULL;
00024
00025 if (TIMES) TIMING(T_IMG2DISK);
00026
00027
00028
00029 if (!CHECKIMG(in)) {
00030 MESSAGE('E'," Can't identify image.");
00031 goto the_end;
00032 }
00033
00034 if (NAMES) {
00035 MESSAGE('I',"");
00036 MESSAGE('I',"IMG2DISK");
00037 MESSAGE('I',"");
00038 sprintf(msg," Input image: %s",in->text);
00039 MESSAGE('I',msg);
00040 sprintf(msg," Output disk file: %s",name);
00041 MESSAGE('I',msg);
00042 sprintf(msg," Output format: %d-bit SADIE",nbit);
00043 MESSAGE('I',msg);
00044 MESSAGE('I'," ...............");
00045 }
00046
00047
00048
00049
00050
00051
00052 RANGE(in);
00053
00054 if (nbit != sizeof(PIXEL)*SBPB && (0L > (long)in->gmin || (long)in->gmax > (1L<<nbit) || sizeof(PIXEL)*SBPB < nbit || nbit > sizeof(long)*SBPB)) {
00055 sprintf(msg," Can't pack to %d bits/pixel.",nbit);
00056 MESSAGE('E',msg);
00057 sprintf(msg," Note: You may want to try %d bits/pixel.",sizeof(PIXEL)*SBPB);
00058 MESSAGE('E',msg);
00059 goto the_end;
00060 }
00061 in->nbit = nbit;
00062
00063
00064
00065 nbnd = in->nbnd;
00066 nlin = in->nlin;
00067 npix = in->npix;
00068 nbit = in->nbit;
00069 if (*(short *)array != 1) {
00070
00071
00072
00073
00074 nbnd = SWAP((unsigned char *)&nbnd);
00075 nlin = SWAP((unsigned char *)&nlin);
00076 npix = SWAP((unsigned char *)&npix);
00077 nbit = SWAP((unsigned char *)&nbit);
00078 }
00079
00080
00081
00082
00083
00084 if (!(fp=fopen(name,"w"))) {
00085 sprintf(msg," Can't open file %s.",name);
00086 MESSAGE('E',msg);
00087 goto the_end;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 if (fwrite(&nbnd,sizeof(nbnd),1,fp) != 1) {
00098 sprintf(msg," Can't write number of bands to file %s.",name);
00099 MESSAGE('E',msg);
00100 goto the_end;
00101 }
00102
00103
00104 if (fwrite(&nlin,sizeof(nlin),1,fp) != 1) {
00105 sprintf(msg," Can't write number of lines/band to file %s.",name);
00106 MESSAGE('E',msg);
00107 goto the_end;
00108 }
00109
00110
00111 if (fwrite(&npix,sizeof(npix),1,fp) != 1) {
00112 sprintf(msg," Can't write number of pixels/line to file %s.",name);
00113 MESSAGE('E',msg);
00114 goto the_end;
00115 }
00116
00117
00118 if (fwrite(&nbit,sizeof(nbit),1,fp) != 1) {
00119 sprintf(msg," Can't write number of bits/pixel to file %s.",name);
00120 MESSAGE('E',msg);
00121 goto the_end;
00122 }
00123
00124
00125 nbytes = (long)(sizeof(in->gmin)+sizeof(in->gmax)+sizeof(in->pstr)+sizeof(in->text));
00126 if (fwrite(&(in->gmin),nbytes,1,fp) != 1) {
00127 sprintf(msg," Can't write image header to file %s.",name);
00128 MESSAGE('E',msg);
00129 goto the_end;
00130 }
00131
00132
00133
00134
00135 nbytes = (long)ceil(((double)in->nbnd*(double)in->nlin*(double)in->npix*(double)in->nbit)/(double)SBPB);
00136 if (in->nbit < sizeof(PIXEL)*SBPB) {
00137 PACK(in->nbit,(long)in->nbnd*(long)in->nlin*(long)in->npix,in->data[0][0]);
00138 }
00139
00140 if (fwrite(in->data[0][0],nbytes,1,fp) != 1) {
00141 sprintf(msg," Can't write image data to file %s.",name);
00142 MESSAGE('E',msg);
00143 }
00144
00145 if (in->nbit < sizeof(PIXEL)*SBPB) {
00146 UNPACK(in->nbit,(long)in->nbnd*(long)in->nlin*(long)in->npix,in->data[0][0]);
00147 }
00148
00149
00150
00151
00152 the_end:
00153
00154 if (fp) {
00155 if (fclose(fp)) {
00156 sprintf(msg," Can't close file %s.",name);
00157 MESSAGE('W',msg);
00158 }
00159 }
00160
00161 if (TIMES) TIMING(T_EXIT);
00162
00163 }