1 // This is brl/bpro/core/brip_pro/processes/brip_truncate_nitf_bit_process.cxx
2 #include <bprb/bprb_func_process.h>
3 //:
4 // \file
5 // \brief  process to ignored desired bits from the vxl_unit_16 nitf images
6 // \verbatim
7 //  Modifications
8 //
9 // \endverbatim
10 
11 #include <brip/brip_vil_nitf_ops.h>
12 #include "vil/vil_image_view_base.h"
13 
14 
15 
16 //: global variables
17 namespace brip_truncate_nitf_bit_process_globals
18 {
19   constexpr unsigned n_inputs_ = 3;
20   constexpr unsigned n_outputs_ = 1;
21 }
22 
23 //: constructor
brip_truncate_nitf_bit_process_cons(bprb_func_process & pro)24 bool brip_truncate_nitf_bit_process_cons(bprb_func_process& pro)
25 {
26   using namespace brip_truncate_nitf_bit_process_globals;
27   // input
28   std::vector<std::string> input_types(n_inputs_);
29   input_types[0] = "vil_image_view_base_sptr";    // input vxl_uint_16 image
30   input_types[1] = "bool";                        // truncation option
31   input_types[2] = "bool";                        // option to scale the image as truncating from vxl_uint_16 to byte image
32   // output
33   std::vector<std::string> output_types(n_outputs_);
34   output_types[0] = "vil_image_view_base_sptr";  // output truncated image
35 
36   return pro.set_input_types(input_types) && pro.set_output_types(output_types);
37 }
38 
39 //: execute the process
brip_truncate_nitf_bit_process(bprb_func_process & pro)40 bool brip_truncate_nitf_bit_process(bprb_func_process& pro)
41 {
42   using namespace brip_truncate_nitf_bit_process_globals;
43 
44   // sanity check
45   if (pro.n_inputs() != n_inputs_) {
46     std::cout << pro.name() << ": The input number should be " << n_inputs_ << std::endl;
47     return false;
48   }
49 
50   // get the input
51   unsigned i = 0;
52   vil_image_view_base_sptr in_img_ptr = pro.get_input<vil_image_view_base_sptr>(i++);
53   bool is_byte = pro.get_input<bool>(i++);
54   bool is_scale = pro.get_input<bool>(i++);
55   if (in_img_ptr->pixel_format() != VIL_PIXEL_FORMAT_UINT_16) {
56     std::cout << pro.name() << ": Unsupported Pixel Format = " << in_img_ptr->pixel_format() << std::endl;
57     return false;
58   }
59 
60   auto* in_img = static_cast<vil_image_view<vxl_uint_16>*>(in_img_ptr.as_pointer());
61   unsigned ni = in_img->ni();
62   unsigned nj = in_img->nj();
63   unsigned np = in_img->nplanes();
64   // being truncation
65   if (is_byte) {
66     std::cout << "truncate to byte image" << std::endl;
67     // truncate the input 16 bits image to a byte image by ignoring the most significant 5 bits and less significant 3 bits
68     auto* out_img = new vil_image_view<vxl_byte>(ni,nj,np);
69     if (is_scale) {
70       if (!brip_vil_nitf_ops::scale_nitf_bits(*in_img, *out_img)) {
71         std::cout << pro.name() << ": scale nitf image from " << in_img_ptr->pixel_format() << " to " << out_img->pixel_format() << " failed!" << std::endl;
72         return false;
73       }
74     }
75     else {
76       if (!brip_vil_nitf_ops::truncate_nitf_bits(*in_img, *out_img)) {
77         std::cout << pro.name() << ": truncate nitf image from " << in_img_ptr->pixel_format() << " to " << out_img->pixel_format() << " failed!" << std::endl;
78       }
79     }
80     // output
81     pro.set_output_val<vil_image_view_base_sptr>(0, vil_image_view_base_sptr(out_img));
82     return true;
83   }
84   else {
85     std::cout << "truncate to short image" << std::endl;
86     // truncate the input image by ignoring the most significant 5 bits
87     auto* out_img = new vil_image_view<vxl_uint_16>(ni, nj, np);
88     if (!brip_vil_nitf_ops::truncate_nitf_bits(*in_img, *out_img)) {
89       std::cout << pro.name() << ": truncating image from " << in_img->pixel_format() << " to " << out_img->pixel_format() << " failed!" << std::endl;
90       return false;
91     }
92     // output
93     pro.set_output_val<vil_image_view_base_sptr>(0, vil_image_view_base_sptr(out_img));
94     return true;
95   }
96 }
97