1 /*
2  * Tool to apply any logical operations on pairs of masks
3  * using a general 'truth table' rule
4  */
5 
6 #include <string>
7 #include <iostream>
8 #include <exception>
9 #ifdef _MSC_VER
10 #  include "vcl_msvc_warnings.h"
11 #endif
12 #include "vul/vul_arg.h"
13 #include <mbl/mbl_mask.h>
14 
main(int argc,char ** argv)15 int main(int argc, char **argv)
16 {
17   vul_arg_base::set_help_description(
18     "Apply a logical operation on a pair of mask files.\n"
19     "--------------------------------------------------\n"
20     "Uses an 'operation' string that defines the 'rule' that is applied between the two masks\n"
21     "The operation must consist of 4 characters, each either 0 or 1\n"
22     "This will be applied to each possible combination of A and B values\n"
23     "For example, the truth table below performs an AND operation:\n"
24     "\n"
25     "        |-A-|-B-|-OUT-|\n"
26     "        | 0 | 0 |  0  |\n"
27     "        | 0 | 1 |  0  |\n"
28     "        | 1 | 0 |  0  |\n"
29     "        | 1 | 1 |  1  |\n"
30     "\n"
31     "The rule in this case would be '0001'\n"
32     "\n"
33     "Some of the more common logical operations:\n"
34     "   A AND B  = 0001\n"
35     "   A OR B   = 0111\n"
36     "   NOT A    = 1100 (Note: Mask B must still exist despite not being used in NOT A\n"
37     "   NOT B    = 0011        and vice versa)\n"
38     "   A XOR B  = 0110\n"
39     "   A NOR B  = 1000\n"
40     "   A XNOR B = 1001\n"
41     "   A NAND B = 1110\n"
42     "\n"
43   );
44 
45   vul_arg<std::string> maskA_filename(nullptr,"Filename of mask A");
46   vul_arg<std::string> maskB_filename(nullptr,"Filename of mask B");
47   vul_arg<std::string> operation(nullptr,"Operation to apply - see help text for explanation");
48   vul_arg<std::string> maskout_filename(nullptr,"Filename of the output mask");
49   vul_arg_parse(argc, argv);
50 
51   mbl_mask maskA, maskB;
52   mbl_load_mask(maskA, maskA_filename().c_str());
53   mbl_load_mask(maskB, maskB_filename().c_str());
54 
55   try { mbl_mask_logic(maskA, maskB, operation()); }
56   catch (std::exception & e)
57   {
58     std::cout << "An error occurred while carrying out the logic operation.\n" << e.what() << std::endl;
59     return 1;
60   }
61   catch (...)
62   {
63     std::cout << "An unknown error occurred while carrying out the logic operation." << std::endl;
64     return 1;
65   }
66 
67   mbl_save_mask(maskB, maskout_filename().c_str());
68 }
69