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