contrib/mul/vil3d/file_formats/vil3d_analyze_format.h
Go to the documentation of this file.
00001 // This is mul/vil3d/file_formats/vil3d_analyze_format.h
00002 #ifndef vil3d_analyze_format_h_
00003 #define vil3d_analyze_format_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Reader/Writer for analyze format images.
00010 // \author Tim Cootes - Manchester
00011 // (Based on old code whose provenance has been lost in the mists of time...)
00012 
00013 #include <vcl_iosfwd.h>
00014 #include <vil3d/vil3d_file_format.h>
00015 
00016 //: Structure containing analyse header file information.
00017 class vil3d_analyze_header
00018 {
00019  public :
00020   // obligatory
00021   class Key
00022   {
00023    public :
00024     int sizeof_hdr;
00025     char data_type[10];
00026     char db_name[18];
00027     int extents;
00028     short int session_error;
00029     char regular;
00030     char hkey_un0;
00031 
00032     Key() { reset(); }
00033     ~Key() {}
00034 
00035     void reset();
00036   };
00037 
00038   // obligatory
00039   class Dimensions
00040   {
00041    public :
00042     short int dim[8];
00043     short int unused8;
00044     short int unused9;
00045     short int unused10;
00046     short int unused11;
00047     short int unused12;
00048     short int unused13;
00049     short int unused14;
00050     short int datatype;
00051     short int bitpix;
00052     short int dim_un0;
00053     float pixdim[8];
00054     float funused8;
00055     float funused9;
00056     float funused10;
00057     float funused11;
00058     float funused12;
00059     float funused13;
00060     float compressed;
00061     float verified;
00062     int glmax;
00063     int glmin;
00064 
00065     Dimensions() { reset(); }
00066     ~Dimensions() {}
00067 
00068     void reset();
00069   };
00070 
00071   // optional
00072   class History
00073   {
00074    public :
00075     char descrip[80];
00076     char aux_file[24];
00077     char orient;
00078     char originator[10];
00079     char generated[10];
00080     char scannum[10];
00081     char patient_id[10];
00082     char exp_date[10];
00083     char exp_time[10];
00084     char hist_un0[3];
00085     int views;
00086     int vols_added;
00087     int start_field;
00088     int field_skip;
00089     int omax;
00090     int omin;
00091     int smax;
00092     int smin;
00093 
00094     History() { reset(); }
00095     ~History() {}
00096 
00097     void reset();
00098   };
00099 
00100   Key        key;
00101   Dimensions dim;
00102   History    history;
00103 
00104  private:
00105   bool swap_bytes_; // True if bytes need to be swapped
00106 
00107  public:
00108   vil3d_analyze_header() : swap_bytes_(false) {}
00109   ~vil3d_analyze_header() {}
00110 
00111   void reset();
00112 
00113   //: Define format of pixels
00114   enum vil_pixel_format pixel_format() const;
00115 
00116   //: Define format of pixels
00117   void set_pixel_format(enum vil_pixel_format format);
00118 
00119   //: Define number of pixels in each dimension
00120   void set_image_size(unsigned ni, unsigned nj, unsigned nk, unsigned np=1);
00121 
00122   short int ni() const { return dim.dim[1]; }
00123   short int nj() const { return dim.dim[2]; }
00124   short int nk() const { return dim.dim[3]; }
00125 
00126   //: Number of planes (or time points in image sequence)
00127   short int nplanes() const { return dim.dim[4]; }
00128 
00129   float voxel_width_i() const { return dim.pixdim[1]; }
00130   float voxel_width_j() const { return dim.pixdim[2]; }
00131   float voxel_width_k() const { return dim.pixdim[3]; }
00132 
00133   //: Define width of voxels in each dimension
00134   void set_voxel_size(float si, float sj, float sk);
00135 
00136   //: Read in header from given file
00137   bool read_file(const vcl_string& path);
00138 
00139   //: Write header to given file
00140   bool write_file(const vcl_string& path) const;
00141 
00142   void swapBytes(char *data, int size);
00143   bool needSwap() const { return swap_bytes_; }
00144 
00145   //: Print out some parts of header
00146   void print_summary(vcl_ostream& os) const;
00147 };
00148 
00149 //: Print out some parts of header
00150 vcl_ostream& operator<<(vcl_ostream& os, const vil3d_analyze_header&);
00151 
00152 //: Reader/Writer for analyze format images.
00153 class vil3d_analyze_format : public vil3d_file_format
00154 {
00155  public:
00156   vil3d_analyze_format();
00157   //: The destructor must be virtual so that the memory chunk is destroyed.
00158   virtual ~vil3d_analyze_format();
00159 
00160   virtual vil3d_image_resource_sptr make_input_image(const char *) const;
00161 
00162   //: Make a "generic_image" on which put_section may be applied.
00163   // The file may be opened immediately for writing so that a header can be written.
00164   virtual vil3d_image_resource_sptr make_output_image(const char* filename,
00165                                                       unsigned ni,
00166                                                       unsigned nj,
00167                                                       unsigned nk,
00168                                                       unsigned nplanes,
00169                                                       enum vil_pixel_format) const;
00170 
00171 
00172   //: default filename tag for this image.
00173   virtual const char * tag() const {return "hdr";}
00174 };
00175 
00176 //: Object which acts as an interface to an analyze format file
00177 // You can't create one of these yourself.
00178 // Use vil3d_analyze_format instead.
00179 class vil3d_analyze_image: public vil3d_image_resource
00180 {
00181   //: Basename of file (not including .hdr/.img)
00182   vcl_string base_path_;
00183 
00184   //: Header information
00185   vil3d_analyze_header header_;
00186 
00187   //: number of planes
00188   unsigned nplanes_;
00189 
00190   //: Physical Voxel dimensions ( in mm )
00191   float vox_width1_, vox_width2_, vox_width3_;
00192 
00193  public:
00194   //: Create object with given header and base_path, ready for reading/writing
00195   //  Doesn't actually load/save anything until get_copy_view() or put_view() called.
00196   //  Header is assumed to have been loaded/saved by the calling function.
00197   vil3d_analyze_image(const vil3d_analyze_header& header,
00198                       const vcl_string& base_path);
00199 
00200   virtual ~vil3d_analyze_image();
00201 
00202   //: Dimensions:  nplanes x ni x nj x nk.
00203   // This concept is treated as a synonym to components.
00204   virtual unsigned nplanes() const;
00205   //: Dimensions:  nplanes x ni x nj x nk.
00206   // The number of pixels in each row.
00207   virtual unsigned ni() const;
00208   //: Dimensions:  nplanes x ni x nj x nk.
00209   // The number of pixels in each column.
00210   virtual unsigned nj() const;
00211   //: Dimensions:  nplanes x ni x nj x nk.
00212   // The number of slices per image.
00213   virtual unsigned nk() const;
00214 
00215   //: Basename of file (not including .hdr/.img)
00216   const vcl_string& base_path() const { return base_path_; }
00217 
00218   //: Header information
00219   const vil3d_analyze_header& header() { return header_; }
00220 
00221   //: Pixel Format.
00222   virtual enum vil_pixel_format pixel_format() const;
00223 
00224 
00225   //: Create a read/write view of a copy of this data.
00226   // This function will always return a
00227   // multi-plane scalar-pixel view of the data.
00228   // \return 0 if unable to get view of correct size, or if resource is write-only.
00229   virtual vil3d_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
00230                                                    unsigned j0, unsigned nj,
00231                                                    unsigned k0, unsigned nk) const;
00232 
00233   //: Put the data in this view back into the image source.
00234   // The view must be of scalar components. Assign your
00235   // view to a scalar-component view if this is not the case.
00236   // \return false if failed, because e.g. resource is read-only,
00237   // format of view is not correct (if it is a compound pixel type, try
00238   // assigning it to a multi-plane scalar pixel view.)
00239   virtual bool put_view(const vil3d_image_view_base& im,
00240                         unsigned i0, unsigned j0, unsigned k0);
00241 
00242   //: Set the size of the each voxel in the i,j,k directions.
00243   // You can get the voxel sizes via get_properties().
00244   // \return false if underlying image doesn't store pixel sizes.
00245   virtual bool set_voxel_size(float/*i*/,float/*j*/,float/*k*/);
00246 
00247   //: Return a string describing the file format.
00248   // Only file images have a format, others return 0
00249   virtual char const* file_format() const { return "analyze"; }
00250 
00251   //: Extra property information
00252   // This will just return the property of the first slice in the list.
00253   virtual bool get_property(char const* label, void* property_value = 0) const;
00254 };
00255 
00256 #endif