contrib/mul/vil3d/file_formats/vil3d_meta_image_format.h
Go to the documentation of this file.
00001 // This is mul/vil3d/file_formats/vil3d_meta_image_format.h
00002 #ifndef vil3d_meta_image_format_h_
00003 #define vil3d_meta_image_format_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Reader/Writer for Meta Image format images.
00010 // \author Chris Wolstenholme - Imorphics
00011 
00012 #include <vil3d/vil3d_file_format.h>
00013 #include <vil/vil_pixel_format.h>
00014 #include <vcl_iosfwd.h>
00015 #include <vcl_string.h>
00016 
00017 class vil3d_meta_image_header
00018 {
00019  public:
00020 
00021   vil3d_meta_image_header(void);
00022   ~vil3d_meta_image_header(void);
00023 
00024   // Setter/getter methods
00025   void set_byte_order_msb(const bool is_msb);
00026   bool byte_order_is_msb(void) const;
00027 
00028   void set_offset(const double off_i, const double off_j, const double off_k);
00029   double offset_i(void) const;
00030   double offset_j(void) const;
00031   double offset_k(void) const;
00032 
00033   void set_vox_size(const double vox_i, const double vox_j, const double vox_k);
00034   double vox_size_i(void) const;
00035   double vox_size_j(void) const;
00036   double vox_size_k(void) const;
00037 
00038   void set_dim_size(const unsigned int ni, const unsigned int nj, const unsigned int nk, const unsigned int np);
00039   unsigned int ni(void) const;
00040   unsigned int nj(void) const;
00041   unsigned int nk(void) const;
00042   unsigned int nplanes(void) const;
00043 
00044   void set_element_type(const vcl_string &elem_type);
00045   const vcl_string &element_type(void) const;
00046 
00047   void set_image_fname(const vcl_string &image_fname);
00048   const vcl_string &image_fname(void) const;
00049 
00050   void set_pixel_format(const vil_pixel_format format);
00051   vil_pixel_format pixel_format(void) const;
00052 
00053   void clear(void);
00054 
00055   // Functions to handle file
00056   bool read_header(const vcl_string &header_fname);
00057   bool write_header(const vcl_string &header_fname) const;
00058 
00059   // Display header details
00060   void print_header(vcl_ostream &os) const;
00061 
00062   // Check if data needs to be swapped when reading/writing
00063   void check_need_swap(void);
00064   bool need_swap(void) const;
00065 
00066  private:
00067 
00068   // Functions to help with header reading/writing
00069   bool check_next_header_line(const vcl_string &nxt_line);
00070   vcl_string get_header_value(const vcl_string &nxt_line);
00071   bool set_header_offset(const vcl_string &offs);
00072   bool set_header_dim_size(const vcl_string &dims);
00073   bool set_header_voxel_size(const vcl_string &vsize);
00074 
00075   // Set to true if the header is valid
00076   bool header_valid_;
00077 
00078   // Header data
00079   bool byte_order_msb_;
00080   double offset_i_, offset_j_, offset_k_;
00081   double vox_size_i_, vox_size_j_, vox_size_k_;
00082   unsigned int dim_size_i_, dim_size_j_, dim_size_k_;
00083   unsigned int nplanes_;
00084   vcl_string elem_type_;
00085   vcl_string im_file_;
00086 
00087   // This is the pixel format of the image, which can differ from the
00088   // file element type
00089   vil_pixel_format pformat_;
00090 
00091   // True if swapping is needed
00092   bool need_swap_;
00093 };
00094 
00095 //: Print out header
00096 vcl_ostream& operator<<(vcl_ostream &os, const vil3d_meta_image_header &header);
00097 
00098 //: Reader/Writer for Meta Image format images.
00099 class vil3d_meta_image_format : public vil3d_file_format
00100 {
00101  public:
00102   vil3d_meta_image_format();
00103   //: The destructor must be virtual so that the memory chunk is destroyed.
00104   virtual ~vil3d_meta_image_format();
00105 
00106   virtual vil3d_image_resource_sptr make_input_image(const char *) const;
00107 
00108   //: Make a "generic_image" on which put_section may be applied.
00109   // The file may be opened immediately for writing so that a header can be written.
00110   virtual vil3d_image_resource_sptr make_output_image(const char* filename,
00111                                                       unsigned ni,
00112                                                       unsigned nj,
00113                                                       unsigned nk,
00114                                                       unsigned nplanes,
00115                                                       vil_pixel_format format) const;
00116 
00117   //: default filename tag for this image.
00118   virtual const char * tag() const {return "mhd";}
00119 };
00120 
00121 // You can't create one of these yourself.
00122 // Use vil3d_meta_image_format instead.
00123 class vil3d_meta_image: public vil3d_image_resource
00124 {
00125  private:
00126   vil3d_meta_image_header header_;
00127   vcl_string fpath_;
00128 
00129   //: Methods for reading/writing image
00130   bool write_image();
00131 
00132  public:
00133    vil3d_meta_image(const vil3d_meta_image_header &header,
00134                     const vcl_string &fname);
00135 
00136   virtual ~vil3d_meta_image();
00137 
00138   //: Dimensions:  nplanes x ni x nj x nk.
00139   // This concept is treated as a synonym to components.
00140   virtual unsigned nplanes() const;
00141   //: Dimensions:  nplanes x ni x nj x nk.
00142   // The number of pixels in each row.
00143   virtual unsigned ni() const;
00144   //: Dimensions:  nplanes x ni x nj x nk.
00145   // The number of pixels in each column.
00146   virtual unsigned nj() const;
00147   //: Dimensions:  nplanes x ni x nj x nk.
00148   // The number of slices per image.
00149   virtual unsigned nk() const;
00150 
00151   //: Get the current header information
00152   const vil3d_meta_image_header &header(void) const;
00153 
00154   //: Pixel Format.
00155   virtual enum vil_pixel_format pixel_format() const;
00156 
00157   //: Set the size of the each voxel in the i,j,k directions.
00158   // You can get the voxel sizes via get_properties().
00159   // \return false if underlying image doesn't store pixel sizes.
00160   virtual bool set_voxel_size(float/*i*/,float/*j*/,float/*k*/);
00161 
00162   //: Set the offset
00163   //  \note also sets the voxel size to vx_i, vx_j and vx_k to ensure consistency
00164   void set_offset(const double i, const double j, const double k,
00165                   const double vx_i, const double vx_j, const double vx_k);
00166 
00167   //: Create a read/write view of a copy of this data.
00168   // This function will always return a
00169   // multi-plane scalar-pixel view of the data.
00170   // \return 0 if unable to get view of correct size, or if resource is write-only.
00171   virtual vil3d_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
00172                                                    unsigned j0, unsigned nj,
00173                                                    unsigned k0, unsigned nk) const;
00174 
00175   //: Put the data in this view back into the image source.
00176   // The view must be of scalar components. Assign your
00177   // view to a scalar-component view if this is not the case.
00178   // \return false if failed, because e.g. resource is read-only,
00179   // format of view is not correct (if it is a compound pixel type, try
00180   // assigning it to a multi-plane scalar pixel view.)
00181   virtual bool put_view(const vil3d_image_view_base& im,
00182                         unsigned i0, unsigned j0, unsigned k0);
00183 
00184   //: Return a string describing the file format.
00185   // Only file images have a format, others return 0
00186   virtual char const* file_format() const { return "meta_image"; }
00187 
00188   //: Extra property information
00189   // This will just return the property of the first slice in the list.
00190   virtual bool get_property(char const* label, void* property_value = 0) const;
00191 };
00192 
00193 #endif