contrib/mul/mbl/tools/mbl_convert_mask_to_indices.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Convert a mask file to a list of indices file.
00004 
00005 #include <vcl_string.h>
00006 #include <vcl_fstream.h>
00007 #include <vul/vul_arg.h>
00008 #include <mbl/mbl_log.h>
00009 #include <mbl/mbl_exception.h>
00010 #include <mbl/mbl_mask.h>
00011 
00012 
00013 //=========================================================================
00014 // Static function to create a static logger when first required
00015 //=========================================================================
00016 static mbl_logger& logger()
00017 {
00018   static mbl_logger l("mul.mbl.tools.convert_mask_to_indices");
00019   return l;
00020 }
00021 
00022 
00023 //=======================================================================
00024 // Write a vector of unsigned values to a stream with a specified delimiter.
00025 //=======================================================================
00026 static void save_indices(const vcl_vector<unsigned>& indices,
00027                          const vcl_string& path,
00028                          const vcl_string& delim="\n")
00029 {
00030   vcl_ofstream afs(path.c_str());
00031   if (!afs)
00032     mbl_exception_throw_os_error(path, "save_indices() could not open file");
00033   for (vcl_vector<unsigned>::const_iterator it=indices.begin(), end=indices.end(); it!=end; ++it)
00034   {
00035     afs << *it << delim;
00036   }
00037   afs.close();
00038 }
00039 
00040 
00041 //=========================================================================
00042 // Main function
00043 //=========================================================================
00044 int main2(int argc, char *argv[])
00045 {
00046   int ret_val=0;
00047   
00048   vul_arg_base::set_help_precis("Convert a boolean mask to a list of zero-based indices.");
00049   vul_arg_base::set_help_description(
00050     "Convert a boolean mask to a list of zero-based indices.\n"
00051     "Indices i will be recorded wherever mask[i] is true.\n"
00052   );
00053 
00054   // Parse command line arguments
00055   vul_arg<vcl_string> mask_file(0, "INPUT mask file");
00056   vul_arg<vcl_string> inds_file(0, "OUTPUT indices file");
00057   vul_arg_parse(argc, argv);
00058 
00059   mbl_mask mask;
00060   mbl_load_mask(mask, mask_file());
00061 
00062   vcl_vector<unsigned> inds;
00063   mbl_mask_to_indices(mask, inds);
00064   save_indices(inds, inds_file());
00065 
00066   return ret_val;
00067 }
00068 
00069 
00070 //=========================================================================
00071 // Main function with exception-handling wrapper and logging
00072 //=========================================================================
00073 int main(int argc, char *argv[])
00074 {
00075   int retcode = 0;
00076 
00077   try
00078   {
00079     mbl_logger::root().load_log_config_file();
00080     retcode = main2(argc, argv);
00081   }
00082   catch (const vcl_runtime_error &e)
00083   {
00084     vcl_cout << "\n";
00085     vcl_cout << "====================================\n";
00086     vcl_cout << "Caught vcl_runtime_error: " << e.what() << "\n";
00087     vcl_cout << "Ending program.\n";
00088     vcl_cout << "====================================\n" << vcl_endl;
00089     MBL_LOG(ERR, logger(), "Caught exception: " << e.what());
00090     retcode = 1;
00091   }
00092   catch (...)
00093   {
00094     vcl_cout << "\n";
00095     vcl_cout << "====================================\n";
00096     vcl_cout << "Caught unknown exception.\n";
00097     vcl_cout << "Ending program.\n";
00098     vcl_cout << "====================================\n" << vcl_endl;
00099     MBL_LOG(ERR, logger(), "Caught unknown exception");
00100     retcode = 2;
00101   }
00102 
00103   return retcode;
00104 }
00105