1 // This is mul/vil3d/file_formats/vil3d_gen_synthetic.cxx
2 //:
3 // \file
4 // \brief Reader for simple images generated on the fly.
5 // \author Ian Scott - Manchester
6 
7 #include "vil3d_gen_synthetic.h"
8 #include <cassert>
9 #ifdef _MSC_VER
10 #  include "vcl_msvc_warnings.h"
11 #endif
12 #include <vil3d/vil3d_image_view.h>
13 #include <vil3d/vil3d_new.h>
14 #include "vil/vil_pixel_format.h"
15 #include "vul/vul_reg_exp.h"
16 #include "vul/vul_string.h"
17 
18 
make_input_image(const char * filename) const19 vil3d_image_resource_sptr vil3d_gen_synthetic_format::make_input_image(const char *filename) const
20 {
21   vul_reg_exp re("^gen:([0-9]+)x([0-9]+)x([0-9]+):([a-z0-9A-Z<>_]+):(-?[0-9\\.]+)$");
22   assert(re.is_valid());
23 
24   if (! re.find(filename))
25     return nullptr;
26 
27   unsigned ni = vul_string_atoi(re.match(1));
28   unsigned nj = vul_string_atoi(re.match(2));
29   unsigned nk = vul_string_atoi(re.match(3));
30 
31   vil_pixel_format pf = vil_pixel_format_from_string(re.match(4).c_str());
32   if (pf == VIL_PIXEL_FORMAT_UNKNOWN)
33   {
34     std::cerr << "ERROR: vil3d_gen_synthetic_format unknown pixel format " << re.match(4) << std::endl;
35     return nullptr;
36   }
37 
38   vil3d_gen_synthetic_pixel_value pv;
39   switch (pf)
40   {
41    case VIL_PIXEL_FORMAT_BOOL:
42     pv.bool_value = vul_string_to_bool(re.match(5));
43     break;
44    case VIL_PIXEL_FORMAT_BYTE:
45     pv.byte_value = static_cast<vxl_byte>(vul_string_atoi(re.match(5)));
46     break;
47    case VIL_PIXEL_FORMAT_SBYTE:
48     pv.sbyte_value = static_cast<vxl_sbyte>(vul_string_atoi(re.match(5)));
49     break;
50    case VIL_PIXEL_FORMAT_INT_16:
51     pv.int_16_value = static_cast<vxl_int_16>(vul_string_atoi(re.match(5)));
52     break;
53    case VIL_PIXEL_FORMAT_UINT_16:
54     pv.uint_16_value = static_cast<vxl_uint_16>(vul_string_atoi(re.match(5)));
55     break;
56    case VIL_PIXEL_FORMAT_INT_32:
57     pv.int_32_value = vul_string_atoi(re.match(5));
58     break;
59    case VIL_PIXEL_FORMAT_UINT_32:
60     pv.uint_32_value = vul_string_atoi(re.match(5));
61     break;
62    case VIL_PIXEL_FORMAT_FLOAT:
63     pv.float_value = static_cast<float>(vul_string_atof(re.match(5)));
64     break;
65    case VIL_PIXEL_FORMAT_DOUBLE:
66     pv.double_value = vul_string_atof(re.match(5));
67     break;
68    default:
69     std::cerr << "ERROR: vil3d_gen_synthetic_format Cannot handle pixel format " << re.match(4) << std::endl;
70     return nullptr;
71   }
72 
73   return new vil3d_gen_synthetic_image(ni, nj, nk, pf, pv);
74 }
75 
76 
77 //: Make a "generic_image" on which put_section may be applied.
78 // The file may be opened immediately for writing so that a header can be written.
79 // The width/height etc are explicitly specified, so that file_format implementors
80 // know what they need to do...
make_output_image(const char *,unsigned,unsigned,unsigned,unsigned,enum vil_pixel_format) const81 vil3d_image_resource_sptr vil3d_gen_synthetic_format::make_output_image(const char* /*filename*/,
82                                                                         unsigned /*ni*/, unsigned /*nj*/,
83                                                                         unsigned /*nk*/, unsigned /*nplanes*/,
84                                                                         enum vil_pixel_format /*format*/) const
85 {
86   std::cerr << "ERROR: Cannot write to generated synthetic images.\n";
87   return nullptr;
88 }
89 
90 
vil3d_gen_synthetic_image(unsigned ni,unsigned nj,unsigned nk,enum vil_pixel_format format,vil3d_gen_synthetic_pixel_value pv)91 vil3d_gen_synthetic_image::vil3d_gen_synthetic_image(
92   unsigned ni,
93   unsigned nj,
94   unsigned nk,
95   enum vil_pixel_format format,
96   vil3d_gen_synthetic_pixel_value pv):
97     ni_(ni), nj_(nj), nk_(nk), format_(format), value_(pv)
98 {
99 }
100 
101 //: Dimensions:  nplanes x ni x nj x nk.
102 // This concept is treated as a synonym to components.
nplanes() const103 unsigned vil3d_gen_synthetic_image::nplanes() const
104 {
105   return vil_pixel_format_num_components(format_);
106 }
107 
108 //: Dimensions:  nplanes x ni x nj x nk.
109 // The number of pixels in each row.
ni() const110 unsigned vil3d_gen_synthetic_image::ni() const
111 {
112   return ni_;
113 }
114 
115 //: Dimensions:  nplanes x ni x nj x nk.
116 // The number of pixels in each column.
nj() const117 unsigned vil3d_gen_synthetic_image::nj() const
118 {
119   return nj_;
120 }
121 
122 //: Dimensions:  nplanes x ni x nj x nk.
123 // The number of slices per image.
nk() const124 unsigned vil3d_gen_synthetic_image::nk() const
125 {
126   return nk_;
127 }
128 
129 //: Pixel Format.
pixel_format() const130 enum vil_pixel_format vil3d_gen_synthetic_image::pixel_format() const
131 {
132   return format_;
133 }
134 
135 //: Get some or all of the volume.
get_copy_view(unsigned i0,unsigned ni,unsigned j0,unsigned nj,unsigned k0,unsigned nk) const136 vil3d_image_view_base_sptr vil3d_gen_synthetic_image::get_copy_view(
137                                unsigned i0, unsigned ni, unsigned j0, unsigned nj,
138                                unsigned k0, unsigned nk) const
139 {
140   if (i0+ni > this->ni() || j0+nj > this->nj() || k0+nk > this->nk()) return nullptr;
141 
142   switch (format_)
143   {
144     case VIL_PIXEL_FORMAT_BOOL:
145     {
146       auto *p =
147         new vil3d_image_view<bool>(ni, nj, nk, nplanes());
148       p->fill(value_.bool_value);
149       return p;
150     }
151     case VIL_PIXEL_FORMAT_SBYTE:
152     {
153       auto *p =
154         new vil3d_image_view<vxl_sbyte>(ni, nj, nk, nplanes());
155       p->fill(value_.sbyte_value);
156       return p;
157     }
158     case VIL_PIXEL_FORMAT_BYTE:
159     {
160       auto *p =
161         new vil3d_image_view<vxl_byte>(ni, nj, nk, nplanes());
162       p->fill(value_.byte_value);
163       return p;
164     }
165     case VIL_PIXEL_FORMAT_INT_16:
166     {
167       auto *p =
168         new vil3d_image_view<vxl_int_16>(ni, nj, nk, nplanes());
169       p->fill(value_.int_16_value);
170       return p;
171     }
172     case VIL_PIXEL_FORMAT_UINT_16:
173     {
174       auto *p =
175         new vil3d_image_view<vxl_uint_16>(ni, nj, nk, nplanes());
176       p->fill(value_.uint_16_value);
177       return p;
178     }
179     case VIL_PIXEL_FORMAT_INT_32:
180     {
181       auto *p =
182         new vil3d_image_view<vxl_int_32>(ni, nj, nk, nplanes());
183       p->fill(value_.int_32_value);
184       return p;
185     }
186     case VIL_PIXEL_FORMAT_UINT_32:
187     {
188       auto *p =
189         new vil3d_image_view<vxl_uint_32>(ni, nj, nk, nplanes());
190       p->fill(value_.uint_32_value);
191       return p;
192     }
193     case VIL_PIXEL_FORMAT_FLOAT:
194     {
195       auto *p =
196         new vil3d_image_view<float>(ni, nj, nk, nplanes());
197       p->fill(value_.float_value);
198       return p;
199     }
200     case VIL_PIXEL_FORMAT_DOUBLE:
201     {
202       auto *p =
203         new vil3d_image_view<double>(ni, nj, nk, nplanes());
204       p->fill(value_.double_value);
205       return p;
206     }
207     default:
208     std::cout<<"ERROR: vil3d_gen_synthetic_format::get_image_data()\n"
209             <<"Can't deal with pixel type " << pixel_format() << std::endl;
210     return nullptr;
211   }
212 }
213 
get_property(char const *,void *) const214 bool vil3d_gen_synthetic_image::get_property(char const *, void *) const
215 {
216   return false;
217 }
218 
219 //: Set the contents of the volume.
put_view(const vil3d_image_view_base &,unsigned,unsigned,unsigned)220 bool vil3d_gen_synthetic_image::put_view(const vil3d_image_view_base&,
221                                          unsigned, unsigned, unsigned)
222 {
223   std::cerr << "ERROR: Cannot write to generated synthetic images.\n";
224   return false;
225 }
226