Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

cpxmult.cc

Go to the documentation of this file.
00001 // cpxmult.c
00002 // baarda::g++ -O cpxmult.c -o cpxmult
00003 // usage: cpxmult infile infile2 [outfile [add]]
00004 // cpxmult: pixelwise complex subtraction of 2 files (if add=1 then adds).
00005 // files: major row order binary (complex) float pixel interleaved
00006 //
00007 // exit levels:
00008 //  0: successful exit;
00009 // -1: wrong input;
00010 //  1: input file cannot be opened;
00011 //  2: output file cannot be opened;
00012 //
00013 // $Revision: 3.5 $  $Date: 2003/04/14 06:25:52 $
00014 // BK 19-Apr-2000
00015 
00016 
00017 using namespace std;
00018 #include <iostream>                             // cout
00019 #include <fstream>                              // file
00020 #include <cstdlib>                              // exit
00021 #include <cmath>                                // atan etc.
00022 #include <complex>                              // conj etc.
00023 #include <cstring>                              // strcat
00024 
00025 void usage(char *programname)
00026   {
00027   cerr << "\nProgram: " << programname 
00028        << " subtracts or adds phase of two complex float files\n"
00029        << "\n  USAGE:\n\t" << programname
00030        << " infile1 infile2 [outfile [add==0]]\n\n"
00031        << "  infile[12] contain complex values a+ib\n"
00032        << "  outfile contains (a1+ib1)*conj(a2+ib2)\n\n" 
00033        << "  If add=1 then phase is added (not conj).\n\n"
00034        << "  EXAMPLE:\n\t" << programname
00035        << " cint.raw cint2.raw subtract.raw"
00036        << "\ndefault output file == infile1.min.infile2\n\n"
00037        << "exit levels: 0:ok; -1: wrong input; 1:ifile; 2:ofile.\n\n\n";
00038   exit(-1);
00039   }
00040 
00041 
00042 int main(int argc, char* argv[])
00043   {
00044   char ident[] = "@(#)Doris software, kampes@geo.tudelft.nl";
00045   const int ONE27   = 127;
00046   char ifile1[ONE27];                           // input file name
00047   char ifile2[ONE27];                           // input file name
00048   char ofile[ONE27] = " ";                      // output filename == "ifile.ml"
00049   bool add = false;                             // take conj by default
00050   const int sizeofelement = sizeof(complex<float>);     // data in file
00051 
00052 // ====== Handle input ======
00053   switch (argc)
00054     {
00055     case 5:
00056       add=true;                                 // should be checked...
00057       //--- fall through ---//
00058     case 4:
00059       strcpy(ofile,argv[3]);                    // output filename arg3
00060       //--- fall through ---//
00061     case 3:
00062       strcpy(ifile2,argv[2]);                   // input filename arg1
00063       //--- fall through ---//
00064     case 2:
00065       strcpy(ifile1,argv[1]);                   // input filename arg1
00066       break; // ---      ---//
00067     default:
00068       usage(argv[0]);
00069     } // switch input
00070 
00071   // ______ Set defaults if required _____
00072   if (!strcmp(ofile," "))                       // nothing specified
00073     {
00074     strcpy(ofile,ifile1);
00075     strcat(ofile,".min.");
00076     strcpy(ofile,ifile2);
00077     }
00078 
00079   // ______ Check / echo input ______
00080   cerr << "Program parameters:\n\t" << argv[0] << " " 
00081        << ifile1 << " " << ifile2 << " " << ofile << " " << add << endl;
00082   if (!strcmp(ofile,ifile1) || !strcmp(ofile,ifile2))
00083     {
00084     cerr << "input file name same as other one: "
00085          << ifile1 << " =? " << ofile << " =? " << ifile2 << endl;
00086     usage(argv[0]);
00087     }
00088 
00089 
00090 // ====== Start complex multiply ======
00091   //ifstream infile1(ifile1, ios::in | ios::nocreate | ios::binary);
00092   ifstream infile1(ifile1, ios::in | ios::binary);
00093   if (!infile1) cerr << "Problem opening file: " << ifile1 << endl, exit(1);
00094   infile1.seekg(0,ios::end);                    // filepointer at end
00095   const int totalbytes1 = infile1.tellg();
00096   infile1.seekg(0,ios::beg);                    // start of file1
00097   //ifstream infile2(ifile2, ios::in | ios::nocreate | ios::binary);
00098   ifstream infile2(ifile2, ios::in | ios::binary);
00099   if (!infile2) cerr << "Problem opening file: " << ifile2 << endl, exit(1);
00100   infile2.seekg(0,ios::end);                    // filepointer at end
00101   const int totalbytes2 = infile2.tellg();
00102   infile2.seekg(0,ios::beg);                    // start of file2
00103   if (totalbytes1!=totalbytes2)
00104     {
00105     cerr <<  "size of file1 != size of file2: "
00106          << totalbytes1 << "!=" << totalbytes2 << endl;
00107     exit(2);
00108     }
00109 
00110   const int numberofpixels = totalbytes1/sizeofelement;
00111   ofstream outfile(ofile, ios::out | ios::binary | ios::trunc);
00112   if (!outfile) cerr << "Problem opening file: " << ofile << endl, exit(2);
00113   int tenpercent = int(floor(numberofpixels/10.));
00114   int percent = 0;
00115 
00116   register complex<float> value1;
00117   register complex<float> value2;
00118   // ______ Good compiler would get rridd of if in for loop, but 2b sure.
00119   if (add)
00120     {
00121     for (register int i=0; i<numberofpixels; ++i)
00122       {
00123       infile1.read((char*)&value1,sizeofelement);
00124       infile2.read((char*)&value2,sizeofelement);
00125       value1 *= value2;
00126       outfile.write((char*)&value1,sizeofelement);
00127       if (!(i%tenpercent))
00128         {
00129         cerr << "\rprocessed: " << percent << "%";
00130         percent += 10;
00131         }
00132       } // loop over all pixels
00133     }
00134   else
00135     {
00136     for (register int i=0; i<numberofpixels; ++i)
00137       {
00138       infile1.read((char*)&value1,sizeofelement);
00139       infile2.read((char*)&value2,sizeofelement);
00140       value1 *= conj(value2);
00141       outfile.write((char*)&value1,sizeofelement);
00142       if (!(i%tenpercent))
00143         {
00144         cerr << "\rprocessed: " << percent << "%";
00145         percent += 10;
00146         }
00147       } // loop over all pixels
00148     }
00149 
00150 
00151 // ====== Tidy up ======
00152   infile1.close();
00153   infile2.close();
00154   outfile.close();
00155   cout << "\n\nThank you for using " << argv[0] << "!\n";
00156   return 0;
00157   } // END
00158 
00159 

Generated on Fri Apr 22 15:57:51 2005 for Doris by doxygen 1.3.6