00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 using namespace std;
00024 #include <iostream>
00025 #include <fstream>
00026 #include <cstdlib>
00027 #include <cmath>
00028 #include <cstring>
00029
00030
00031 void usage(char *programname)
00032 {
00033 cerr << "\nProgram: " << programname
00034 << " wraps float binary file to interval [a,b)\n"
00035 << "\n USAGE:\n\t" << programname
00036 << " infile [a b [ofile]]\n\n"
00037 << " EXAMPLE:\n\t" << programname
00038 << " interferogram.raw -4pi 4pi interf4.raw\n"
00039 << "\ndefault output file == infile.wrap"
00040 << "\ndefault interval [a b) == [-pi pi)\n\n";
00041 exit(-1);
00042 }
00043
00044 int main(int argc, char* argv[])
00045 {
00046
00047 char ident[] = "@(#)wrap: Doris software, $Revision: 3.6 $, $Author: kampes $";
00048 cerr << ident << endl;
00049 const int ONE27 = 127;
00050 const float PI = 4.*atan(1.);
00051 char ifile[ONE27];
00052 char ofile[ONE27] = " ";
00053 char dummy[ONE27];
00054
00055 float a = -999.;
00056 float b = -999.;
00057 register int i;
00058 const int sizeofelement = sizeof(float);
00059
00060
00061 switch (argc)
00062 {
00063 case 3:
00064 usage(argv[0]);
00065 break;
00066
00067 case 5:
00068 strcpy(ofile,argv[4]);
00069
00070
00071 case 4:
00072 strcpy(dummy,argv[3]);
00073 i = strlen(dummy);
00074 if (dummy[i-1] == 'i')
00075 {
00076 if (!strcmp(dummy,"pi"))
00077 b = PI;
00078 else if (!strcmp(dummy,"-pi"))
00079 b = -PI;
00080 else
00081 {
00082 dummy[i-2]='\0';
00083 b = PI*atof(dummy);
00084 }
00085 }
00086 else
00087 {
00088 b = atof(dummy);
00089 }
00090
00091 strcpy(dummy,argv[2]);
00092 i = strlen(dummy);
00093 if (dummy[i-1] == 'i')
00094 {
00095 if (!strcmp(dummy,"pi"))
00096 a = PI;
00097 else if (!strcmp(dummy,"-pi"))
00098 a = -PI;
00099 else
00100 {
00101 dummy[i-2]='\0';
00102 a = PI*atof(dummy);
00103 }
00104 }
00105 else
00106 {
00107 a = atof(dummy);
00108 }
00109
00110
00111 case 2:
00112 strcpy(ifile,argv[1]);
00113 break;
00114
00115 default:
00116 usage(argv[0]);
00117 }
00118
00119
00120 if (abs(a+999.) < 1e-10)
00121 a = -PI;
00122 if (abs(b+999.) < 1e-10)
00123 b = PI;
00124 if (!strcmp(ofile," "))
00125 {
00126 strcpy(ofile,ifile);
00127 strcat(ofile,".wrap");
00128 }
00129
00130
00131 if (a>=b)
00132 {
00133 cerr << "interval (a,b) = (" << a << "," << b
00134 << "): a should be smaller than b.\n";
00135 usage(argv[0]);
00136 }
00137
00138 if (abs(a+b)>1e-10 && abs(a)>1e-10)
00139 {
00140 cerr << "interval (a,b): should be either: a=-b or a=0\n";
00141 usage(argv[0]);
00142 }
00143 if (!strcmp(ofile,ifile))
00144 {
00145 cerr << "input file name same as output file name: "
00146 << ifile << " = " << ofile << endl;
00147 usage(argv[0]);
00148 }
00149
00150 cerr << "Program parameters:\n\t" << argv[0] << " "
00151 << ifile << " " << a << " " << b << " " << ofile << endl;
00152
00153
00154
00155
00156
00157
00158 ifstream image(ifile, ios::in | ios::binary);
00159
00160
00161 image.seekg(0,ios::end);
00162
00163 if (!image) cerr << "Problem opening file: " << ifile << endl, exit(1);
00164 const int totalbytes = image.tellg();
00165 const int numberofpixels = totalbytes/sizeofelement;
00166 image.seekg(0,ios::beg);
00167 ofstream wrapped(ofile, ios::out | ios::binary | ios::trunc);
00168 if (!wrapped) cerr << "Problem opening file: " << ofile << endl, exit(2);
00169
00170 register float phase;
00171 const float bereik = b-a;
00172 const float normal = (.5*bereik)/PI;
00173 bool ais0 = false;
00174 if (abs(a)<1e-10)
00175 ais0 = true;
00176
00177 for (i=0; i<numberofpixels; ++i)
00178 {
00179 image.read((char*)&phase,sizeofelement);
00180
00181
00182 phase /= normal;
00183 phase = atan2(sin(phase),cos(phase));
00184 phase *= normal;
00185 if (ais0)
00186 if (phase < 0)
00187 phase += bereik;
00188
00189 wrapped.write((char*)&phase,sizeofelement);
00190 }
00191
00192
00193 image.close();
00194 wrapped.close();
00195
00196 cout << "\n\nAll done!\n";
00197 return 0;
00198 }