1 // This is brl/bpro/core/vil_pro/processes/vil_convert_pixel_type_process.cxx
2 #include <iostream>
3 #include <string>
4 #include <bprb/bprb_func_process.h>
5 //:
6 // \file
7 // \brief Converts floating point image (0,1.0) to byte image (0,255) for compression/saveability reasons
8 
9 #include <bprb/bprb_parameters.h>
10 #ifdef _MSC_VER
11 #  include "vcl_msvc_warnings.h"
12 #endif
13 #include "vil/vil_image_view_base.h"
14 #include "vil/vil_math.h"
15 #include "vil/vil_convert.h"
16 
17 //: Constructor
vil_convert_pixel_type_process_cons(bprb_func_process & pro)18 bool vil_convert_pixel_type_process_cons(bprb_func_process& pro)
19 {
20   //this process takes two inputs:
21   std::vector<std::string> input_types;
22   input_types.emplace_back("vil_image_view_base_sptr");
23   input_types.emplace_back("vcl_string");
24 
25   //this process has 1 output
26   // output(0): the output image with the specified number of planes
27   std::vector<std::string> output_types;
28   output_types.emplace_back("vil_image_view_base_sptr");  // label image
29 
30   return pro.set_input_types(input_types)
31       && pro.set_output_types(output_types);
32 }
33 
34 //: Execute the process
vil_convert_pixel_type_process(bprb_func_process & pro)35 bool vil_convert_pixel_type_process(bprb_func_process& pro)
36 {
37   // Sanity check
38   if (pro.n_inputs()<2) {
39     std::cout << "vil_convert_pixel_type_process: The input number should be 2" << std::endl;
40     return false;
41   }
42   unsigned i=0;
43 
44   //Retrieve image from input
45   vil_image_view_base_sptr img = pro.get_input<vil_image_view_base_sptr>(i++);
46 
47   //retrieve output type
48   std::string out_type = pro.get_input<std::string>(i++);
49 
50   ////////////////////////////////////////////////////////////////////
51   //Convert to float image
52   ////////////////////////////////////////////////////////////////////
53   vil_image_view<float> fimage;
54   if (img->pixel_format() == VIL_PIXEL_FORMAT_RGBA_BYTE ) {
55     vil_image_view_base_sptr plane_image = vil_convert_to_n_planes(4, img);
56     fimage = *vil_convert_cast(float(), plane_image);
57     vil_math_scale_values(fimage, 1.0/255.0);
58   }
59   else {
60     //can use convert cast
61     fimage = *vil_convert_cast(float(), img);
62 
63     // convert input image to float in range from 0 to 1
64     if (img->pixel_format() == VIL_PIXEL_FORMAT_BYTE)
65       vil_math_scale_values(fimage,1.0/255.0);
66     if (img->pixel_format() == VIL_PIXEL_FORMAT_UINT_16)
67       vil_math_scale_values(fimage,1.0/4096);
68   }
69   unsigned ni = fimage.ni();
70   unsigned nj = fimage.nj();
71   unsigned nplanes = fimage.nplanes();
72 
73   ////////////////////////////////////////////////////////////////////
74   //return image of same type (convert back)
75   ////////////////////////////////////////////////////////////////////
76   if (out_type=="float") {
77     auto* bimage = new vil_image_view<float>(ni, nj, nplanes);
78     vil_convert_cast(fimage, *bimage);
79     pro.set_output_val<vil_image_view_base_sptr>(0, bimage);
80     return true;
81   }
82   else if (out_type=="byte") {
83     vil_math_scale_values( fimage, 255.0 );
84     auto* bimage = new vil_image_view<vxl_byte>(ni, nj, nplanes);
85     vil_convert_cast(fimage, *bimage);
86     pro.set_output_val<vil_image_view_base_sptr>(0, bimage);
87     return true;
88   }
89   else if (out_type=="uint16") {
90     vil_math_scale_values( fimage, 65535.0 );
91     auto* bimage = new vil_image_view<vxl_uint_16>(ni, nj, nplanes);
92     vil_convert_cast(fimage, *bimage);
93     pro.set_output_val<vil_image_view_base_sptr>(0, bimage);
94     return true;
95   }
96   else if (out_type=="rgba")    {
97     vil_math_scale_values( fimage, 255.0);
98     auto* bimage = new vil_image_view<vxl_byte>(ni, nj, nplanes);
99     vil_convert_cast(fimage, *bimage);
100     pro.set_output_val<vil_image_view_base_sptr>(0, vil_convert_to_component_order(bimage));
101     return true;
102   }
103   else if (out_type=="grey")   {
104     auto* floatimg = new vil_image_view<float>(img->ni(), img->nj());
105     if (img->nplanes() == 3 || img->nplanes() == 4) {
106       auto* inImg = dynamic_cast<vil_image_view<vxl_byte>* >(img.ptr());
107       vil_image_view<float>     greyImg(img->ni(), img->nj());
108       vil_convert_planes_to_grey<vxl_byte, float>(*inImg, greyImg);
109 
110       //stretch it into 0-1 range
111       vil_convert_stretch_range_limited(greyImg, *floatimg, 0.0f, 255.0f, 0.0f, 1.0f);
112     }
113     pro.set_output_val<vil_image_view_base_sptr>(0, floatimg);
114     return true;
115   }
116   else
117     return false;
118 }
119