contrib/mul/mbl/tools/mbl_convert_indices_to_mask.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Convert a list of indices to a mask.
00004 
00005 #include <vcl_algorithm.h>
00006 #include <vcl_string.h>
00007 #include <vcl_fstream.h>
00008 #include <vcl_stdexcept.h>
00009 #include <vcl_iterator.h>
00010 #include <vcl_iostream.h>
00011 #include <vul/vul_arg.h>
00012 #include <mbl/mbl_log.h>
00013 #include <mbl/mbl_mask.h>
00014 #include <mbl/mbl_exception.h>
00015 
00016 
00017 //=========================================================================
00018 // Static function to create a static logger when first required
00019 //=========================================================================
00020 static mbl_logger& logger()
00021 {
00022   static mbl_logger l("mul.mbl.tools.convert_indices_to_mask");
00023   return l;
00024 }
00025 
00026 
00027 //=========================================================================
00028 // Load a vector of unsigned from named file
00029 //=========================================================================
00030 static void load_indices(vcl_vector<unsigned>& indices,
00031                          const vcl_string& path)
00032 {
00033   vcl_ifstream ifs(path.c_str());
00034   if (!ifs)
00035     mbl_exception_throw_os_error(path, "load_indices() could not open file");
00036   indices.assign(vcl_istream_iterator<unsigned>(ifs), vcl_istream_iterator<unsigned>());
00037 
00038   if (indices.empty())
00039     throw mbl_exception_parse_error("Could not parse indices file.");
00040 }
00041 
00042 
00043 //=========================================================================
00044 // Main function
00045 //=========================================================================
00046 int main2(int argc, char *argv[])
00047 {
00048   int ret_val=0;
00049 
00050   vul_arg_base::set_help_precis("Convert a list of zero-based indices to a boolean mask.");
00051   vul_arg_base::set_help_description(
00052     "Convert a list of zero-based indices to a boolean mask.\n"
00053     "Mask will be false everywhere except at the specified indices.\n"
00054     "By default, the mask length will be defined by the highest index present,\n"
00055     "unless you specify the -n option."
00056   );
00057 
00058   // Parse command line arguments
00059   vul_arg<vcl_string> inds_file(0, "INPUT indices file");
00060   vul_arg<unsigned> n("-n", "Length of mask; default is to use highest index in indices file");
00061   vul_arg<vcl_string> mask_file(0, "OUTPUT mask file");
00062   vul_arg_parse(argc, argv);
00063 
00064   vcl_vector<unsigned> inds;
00065   load_indices(inds, inds_file());
00066 
00067   unsigned num=0;
00068   if (n.set())
00069   {
00070     num = n();
00071   }
00072   else
00073   {
00074     num = 1 + *vcl_max_element(inds.begin(), inds.end());
00075   }
00076 
00077   mbl_mask mask;
00078   mbl_indices_to_mask(inds, num, mask);
00079 
00080   mbl_save_mask(mask, mask_file());
00081 
00082   return ret_val;
00083 }
00084 
00085 
00086 //=========================================================================
00087 // Main function with exception-handling wrapper and logging
00088 //=========================================================================
00089 int main(int argc, char *argv[])
00090 {
00091   int retcode = 0;
00092 
00093   try
00094   {
00095     mbl_logger::root().load_log_config_file();
00096     retcode = main2(argc, argv);
00097   }
00098   catch (const vcl_runtime_error &e)
00099   {
00100     vcl_cout << "\n";
00101     vcl_cout << "====================================\n";
00102     vcl_cout << "Caught vcl_runtime_error: " << e.what() << "\n";
00103     vcl_cout << "Ending program.\n";
00104     vcl_cout << "====================================\n" << vcl_endl;
00105     MBL_LOG(ERR, logger(), "Caught exception: " << e.what());
00106     retcode = 1;
00107   }
00108   catch (...)
00109   {
00110     vcl_cout << "\n";
00111     vcl_cout << "====================================\n";
00112     vcl_cout << "Caught unknown exception.\n";
00113     vcl_cout << "Ending program.\n";
00114     vcl_cout << "====================================\n" << vcl_endl;
00115     MBL_LOG(ERR, logger(), "Caught unknown exception");
00116     retcode = 2;
00117   }
00118 
00119   return retcode;
00120 }
00121