00001 #include <stdio.h>
00002 #include "sadie.h"
00003 #include "mosaic.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 void MOSAIC_BYTE_EXPORT (in,name)
00016 IMAGE_BYTE *in;
00017 char *name;
00018
00019 {
00020 static char array[2] = { 0x00, 0x01 };
00021 char msg[SLEN];
00022 short nbnd, nbit;
00023 unsigned short nlin, npix;
00024 unsigned short j,k;
00025 long nbytes;
00026 PIXEL_BYTE *column=NULL;
00027 PIXEL gmin, gmax;
00028 FILE *fp=NULL;
00029
00030
00031
00032
00033
00034 if (!CHECKIMG_BYTE(in)) {
00035 MESSAGE('E'," Can't identify image.");
00036 goto the_end;
00037 }
00038
00039 if (NAMES) {
00040 MESSAGE('I',"");
00041 MESSAGE('I',"MOSAIC_BYTE_EXPORT");
00042 MESSAGE('I',"");
00043 sprintf(msg," Input image: %s",in->text);
00044 MESSAGE('I',msg);
00045 sprintf(msg," Output disk file: %s",name);
00046 MESSAGE('I',msg);
00047 sprintf(msg," Output format: 8-bit BIP in MOSAIC_IMAGE format");
00048 MESSAGE('I',msg);
00049 MESSAGE('I'," ...............");
00050 }
00051
00052
00053 if (in->nbnd > 1) {
00054 MESSAGE('I'," Only first band of multi-band image will be written to disk.");
00055 }
00056
00057
00058 in->nbit = 8;
00059
00060
00061 nbnd = 1;
00062 nlin = in->nlin;
00063 npix = in->npix;
00064 nbit = in->nbit;
00065 if (*(short *)array != 1) {
00066
00067
00068
00069
00070 nbnd = SWAP((unsigned char *)&nbnd);
00071 nlin = SWAP((unsigned char *)&nlin);
00072 npix = SWAP((unsigned char *)&npix);
00073 nbit = SWAP((unsigned char *)&nbit);
00074 }
00075
00076
00077
00078
00079 printf("Exporting mosaic of size %dx%d\n",in->npix,in->nlin);
00080
00081
00082 if (!(fp=fopen(name,"w"))) {
00083 sprintf(msg," Can't open file %s.",name);
00084 MESSAGE('E',msg);
00085 goto the_end;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095 if (fwrite(&nbnd,sizeof(nbnd),1,fp) != 1) {
00096 sprintf(msg," Can't write number of bands to file %s.",name);
00097 MESSAGE('E',msg);
00098 goto the_end;
00099 }
00100
00101
00102 if (fwrite(&nlin,sizeof(nlin),1,fp) != 1) {
00103 sprintf(msg," Can't write number of lines/band to file %s.",name);
00104 MESSAGE('E',msg);
00105 goto the_end;
00106 }
00107
00108
00109 if (fwrite(&npix,sizeof(npix),1,fp) != 1) {
00110 sprintf(msg," Can't write number of PIXEL_BYTEs/line to file %s.",name);
00111 MESSAGE('E',msg);
00112 goto the_end;
00113 }
00114
00115
00116 if (fwrite(&nbit,sizeof(nbit),1,fp) != 1) {
00117 sprintf(msg," Can't write number of bits/PIXEL_BYTE to file %s.",name);
00118 MESSAGE('E',msg);
00119 goto the_end;
00120 }
00121
00122 gmin = (PIXEL)in->gmin;
00123 if (fwrite(&gmin,sizeof(gmin),1,fp) != 1) {
00124 sprintf(msg," Can't write gmin to file %s.",name);
00125 MESSAGE('E',msg);
00126 goto the_end;
00127 }
00128 gmax = (PIXEL)in->gmax;
00129 if (fwrite(&gmax,sizeof(gmax),1,fp) != 1) {
00130 sprintf(msg," Can't write gmax to file %s.",name);
00131 MESSAGE('E',msg);
00132 goto the_end;
00133 }
00134
00135
00136 nbytes = (long)(sizeof(in->pstr)+sizeof(in->text));
00137 if (fwrite(&(in->pstr),nbytes,1,fp) != 1) {
00138 sprintf(msg," Can't write image header to file %s.",name);
00139 MESSAGE('E',msg);
00140 goto the_end;
00141 }
00142
00143
00144
00145
00146 nbytes = (long)in->nlin;
00147 if ((column=(PIXEL_BYTE *)malloc((long)in->nlin * sizeof(PIXEL_BYTE))) == NULL) {
00148 MESSAGE('E'," Memory request failed.");
00149 goto the_end;
00150 }
00151
00152 for (k = 0; k < in->npix; k++) {
00153 for (j=0; j < in->nlin; j++) {
00154 column[j] = in->data[0][j][k];
00155 }
00156
00157 if (fwrite(column,nbytes,1,fp) != 1) {
00158 sprintf(msg," Can't write image data to file %s.",name);
00159 MESSAGE('E',msg);
00160 }
00161 }
00162
00163
00164
00165 the_end:
00166
00167 if (column) free(column);
00168
00169 if (fp) {
00170 if (fclose(fp)) {
00171 sprintf(msg," Can't close file %s.",name);
00172 MESSAGE('W',msg);
00173 }
00174 }
00175
00176
00177
00178 }