contrib/mul/vil3d/file_formats/vil3d_gen_synthetic.cxx
Go to the documentation of this file.
00001 // This is mul/vil3d/file_formats/vil3d_gen_synthetic.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief Reader for simple images generated on the fly.
00008 // \author Ian Scott - Manchester
00009 
00010 #include "vil3d_gen_synthetic.h"
00011 #include <vcl_cassert.h>
00012 #include <vil3d/vil3d_image_view.h>
00013 #include <vil3d/vil3d_new.h>
00014 #include <vil/vil_pixel_format.h>
00015 #include <vul/vul_reg_exp.h>
00016 #include <vul/vul_string.h>
00017 
00018 
00019 vil3d_image_resource_sptr vil3d_gen_synthetic_format::make_input_image(const char *filename) const
00020 {
00021   vul_reg_exp re("^gen:([0-9]+)x([0-9]+)x([0-9]+):([a-z0-9A-Z<>_]+):(-?[0-9\\.]+)$");
00022   assert(re.is_valid());
00023 
00024   if (! re.find(filename))
00025     return 0;
00026 
00027   unsigned ni = vul_string_atoi(re.match(1));
00028   unsigned nj = vul_string_atoi(re.match(2));
00029   unsigned nk = vul_string_atoi(re.match(3));
00030 
00031   vil_pixel_format pf = vil_pixel_format_from_string(re.match(4).c_str());
00032   if (pf == VIL_PIXEL_FORMAT_UNKNOWN)
00033   {
00034     vcl_cerr << "ERROR: vil3d_gen_synthetic_format unknown pixel format " << re.match(4) << vcl_endl;
00035     return 0;
00036   }
00037 
00038   vil3d_gen_synthetic_pixel_value pv;
00039   switch (pf)
00040   {
00041    case VIL_PIXEL_FORMAT_BOOL:
00042     pv.bool_value = vul_string_to_bool(re.match(5));
00043     break;
00044    case VIL_PIXEL_FORMAT_BYTE:
00045     pv.byte_value = static_cast<vxl_byte>(vul_string_atoi(re.match(5)));
00046     break;
00047    case VIL_PIXEL_FORMAT_SBYTE:
00048     pv.sbyte_value = static_cast<vxl_sbyte>(vul_string_atoi(re.match(5)));
00049     break;
00050    case VIL_PIXEL_FORMAT_INT_16:
00051     pv.int_16_value = static_cast<vxl_int_16>(vul_string_atoi(re.match(5)));
00052     break;
00053    case VIL_PIXEL_FORMAT_UINT_16:
00054     pv.uint_16_value = static_cast<vxl_uint_16>(vul_string_atoi(re.match(5)));
00055     break;
00056    case VIL_PIXEL_FORMAT_INT_32:
00057     pv.int_32_value = vul_string_atoi(re.match(5));
00058     break;
00059    case VIL_PIXEL_FORMAT_UINT_32:
00060     pv.uint_32_value = vul_string_atoi(re.match(5));
00061     break;
00062    case VIL_PIXEL_FORMAT_FLOAT:
00063     pv.float_value = static_cast<float>(vul_string_atof(re.match(5)));
00064     break;
00065    case VIL_PIXEL_FORMAT_DOUBLE:
00066     pv.double_value = vul_string_atof(re.match(5));
00067     break;
00068    default:
00069     vcl_cerr << "ERROR: vil3d_gen_synthetic_format Cannot handle pixel format " << re.match(4) << vcl_endl;
00070     return 0;
00071   }
00072 
00073   return new vil3d_gen_synthetic_image(ni, nj, nk, pf, pv);
00074 }
00075 
00076 
00077 //: Make a "generic_image" on which put_section may be applied.
00078 // The file may be opened immediately for writing so that a header can be written.
00079 // The width/height etc are explicitly specified, so that file_format implementors
00080 // know what they need to do...
00081 vil3d_image_resource_sptr vil3d_gen_synthetic_format::make_output_image(const char* /*filename*/,
00082                                                                         unsigned /*ni*/, unsigned /*nj*/,
00083                                                                         unsigned /*nk*/, unsigned /*nplanes*/,
00084                                                                         enum vil_pixel_format /*format*/) const
00085 {
00086   vcl_cerr << "ERROR: Cannot write to generated synthetic images.\n";
00087   return 0;
00088 }
00089 
00090 
00091 vil3d_gen_synthetic_image::vil3d_gen_synthetic_image(
00092   unsigned ni,
00093   unsigned nj,
00094   unsigned nk,
00095   enum vil_pixel_format format,
00096   vil3d_gen_synthetic_pixel_value pv):
00097     ni_(ni), nj_(nj), nk_(nk), format_(format), value_(pv)
00098 {
00099 }
00100 
00101 //: Dimensions:  nplanes x ni x nj x nk.
00102 // This concept is treated as a synonym to components.
00103 unsigned vil3d_gen_synthetic_image::nplanes() const
00104 {
00105   return vil_pixel_format_num_components(format_);
00106 }
00107 
00108 //: Dimensions:  nplanes x ni x nj x nk.
00109 // The number of pixels in each row.
00110 unsigned vil3d_gen_synthetic_image::ni() const
00111 {
00112   return ni_;
00113 }
00114 
00115 //: Dimensions:  nplanes x ni x nj x nk.
00116 // The number of pixels in each column.
00117 unsigned vil3d_gen_synthetic_image::nj() const
00118 {
00119   return nj_;
00120 }
00121 
00122 //: Dimensions:  nplanes x ni x nj x nk.
00123 // The number of slices per image.
00124 unsigned vil3d_gen_synthetic_image::nk() const
00125 {
00126   return nk_;
00127 }
00128 
00129 //: Pixel Format.
00130 enum vil_pixel_format vil3d_gen_synthetic_image::pixel_format() const
00131 {
00132   return format_;
00133 }
00134 
00135 //: Get some or all of the volume.
00136 vil3d_image_view_base_sptr vil3d_gen_synthetic_image::get_copy_view(
00137                                unsigned i0, unsigned ni, unsigned j0, unsigned nj,
00138                                unsigned k0, unsigned nk) const
00139 {
00140   if (i0+ni > this->ni() || j0+nj > this->nj() || k0+nk > this->nk()) return 0;
00141 
00142   switch (format_)
00143   {
00144     case VIL_PIXEL_FORMAT_BOOL:
00145     {
00146       vil3d_image_view<bool> *p =
00147         new vil3d_image_view<bool>(ni, nj, nk, nplanes());
00148       p->fill(value_.bool_value);
00149       return p;
00150     }
00151     case VIL_PIXEL_FORMAT_SBYTE:
00152     {
00153       vil3d_image_view<vxl_sbyte> *p =
00154         new vil3d_image_view<vxl_sbyte>(ni, nj, nk, nplanes());
00155       p->fill(value_.sbyte_value);
00156       return p;
00157     }
00158     case VIL_PIXEL_FORMAT_BYTE:
00159     {
00160       vil3d_image_view<vxl_byte> *p =
00161         new vil3d_image_view<vxl_byte>(ni, nj, nk, nplanes());
00162       p->fill(value_.byte_value);
00163       return p;
00164     }
00165     case VIL_PIXEL_FORMAT_INT_16:
00166     {
00167       vil3d_image_view<vxl_int_16> *p =
00168         new vil3d_image_view<vxl_int_16>(ni, nj, nk, nplanes());
00169       p->fill(value_.int_16_value);
00170       return p;
00171     }
00172     case VIL_PIXEL_FORMAT_UINT_16:
00173     {
00174       vil3d_image_view<vxl_uint_16> *p =
00175         new vil3d_image_view<vxl_uint_16>(ni, nj, nk, nplanes());
00176       p->fill(value_.uint_16_value);
00177       return p;
00178     }
00179     case VIL_PIXEL_FORMAT_INT_32:
00180     {
00181       vil3d_image_view<vxl_int_32> *p =
00182         new vil3d_image_view<vxl_int_32>(ni, nj, nk, nplanes());
00183       p->fill(value_.int_32_value);
00184       return p;
00185     }
00186     case VIL_PIXEL_FORMAT_UINT_32:
00187     {
00188       vil3d_image_view<vxl_uint_32> *p =
00189         new vil3d_image_view<vxl_uint_32>(ni, nj, nk, nplanes());
00190       p->fill(value_.uint_32_value);
00191       return p;
00192     }
00193     case VIL_PIXEL_FORMAT_FLOAT:
00194     {
00195       vil3d_image_view<float> *p =
00196         new vil3d_image_view<float>(ni, nj, nk, nplanes());
00197       p->fill(value_.float_value);
00198       return p;
00199     }
00200     case VIL_PIXEL_FORMAT_DOUBLE:
00201     {
00202       vil3d_image_view<double> *p =
00203         new vil3d_image_view<double>(ni, nj, nk, nplanes());
00204       p->fill(value_.double_value);
00205       return p;
00206     }
00207     default:
00208     vcl_cout<<"ERROR: vil3d_gen_synthetic_format::get_image_data()\n"
00209             <<"Can't deal with pixel type " << pixel_format() << vcl_endl;
00210     return 0;
00211   }
00212 }
00213 
00214 bool vil3d_gen_synthetic_image::get_property(char const *, void *) const
00215 {
00216   return false;
00217 }
00218 
00219 //: Set the contents of the volume.
00220 bool vil3d_gen_synthetic_image::put_view(const vil3d_image_view_base&,
00221                                          unsigned, unsigned, unsigned)
00222 {
00223   vcl_cerr << "ERROR: Cannot write to generated synthetic images.\n";
00224   return false;
00225 }
00226