contrib/mul/vimt3d/vimt3d_vil3d_v3m.h
Go to the documentation of this file.
00001 // This is mul/vimt3d/vimt3d_vil3d_v3m.h
00002 #ifndef vimt3d_vil3d_v3m_h_
00003 #define vimt3d_vil3d_v3m_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Reader/Writer for v3m format images.
00010 // \author Ian Scott - Imorphics
00011 // v3m is a VXL specific format designed to store all the information that
00012 // can be stored in a vimt3d image, using run-length-encryption.
00013 // We don't recommend its use in general,
00014 // having no wish to add to the plethora of badly designed image formats
00015 // out there. However, a means of being able to save, and reload a full
00016 // vil3d/vimt3d mask or label image using the standard vil3d_load_* API
00017 // is very useful.
00018 // The file format currently is different to that provided by v3i or the
00019 // default vsl serialisation scheme, having learnt lessons from the evolution
00020 // of v3i. There is an extra v3m-specific magic number to avoid confusion
00021 // with other vsl data files, and a version number to allow for a change
00022 // in format.
00023 //
00024 // To use the v3m format with vil3d_load, vil3d_save, etc add
00025 // the following code at the start of your program
00026 // \verbatim
00027 //  vil3d_file_format::add_format(new vimt3d_vil3d_v3m_format);
00028 // \endverbatim
00029 
00030 
00031 #include <vil3d/vil3d_file_format.h>
00032 #include <vimt3d/vimt3d_image_3d.h>
00033 #include <vcl_iosfwd.h>
00034 #include <vcl_memory.h>
00035 
00036 //: Reader/Writer for v3m format images, specialised for label or mask images.
00037 //
00038 // To use the v3m format with vil3d_load, vil3d_save, etc add
00039 // the following code at the start of your program
00040 // \verbatim
00041 //  vil3d_file_format::add_format(new vimt3d_vil3d_v3m_format);
00042 // \endverbatim
00043 class vimt3d_vil3d_v3m_format: public vil3d_file_format
00044 {
00045  public:
00046   vimt3d_vil3d_v3m_format() {}
00047   //: The destructor must be virtual so that the memory chunk is destroyed.
00048   virtual ~vimt3d_vil3d_v3m_format() {}
00049 
00050   virtual vil3d_image_resource_sptr make_input_image(const char *) const;
00051 
00052   //: Make a "generic_image" on which put_section may be applied.
00053   // The file may be opened immediately for writing so that a header can be written.
00054   virtual vil3d_image_resource_sptr make_output_image(const char* filename,
00055                                                       unsigned ni,
00056                                                       unsigned nj,
00057                                                       unsigned nk,
00058                                                       unsigned nplanes,
00059                                                       enum vil_pixel_format) const;
00060 
00061   //: default filename tag for this image.
00062   virtual const char * tag() const {return "v3m";}
00063 
00064   //: The magic number to identify a vsl stream as a v3m image.
00065   // You can create/read a v3m image using vsl by opening the stream,
00066   // reading/writing magic_number(), then reading/writing a pointer to a vimt_image.
00067   static unsigned magic_number();
00068 };
00069 
00070 
00071 // You can't create one of these yourself.
00072 // Use vimt3d_vil3d_v3m_format instead.
00073 //
00074 // To use the v3m format with vil3d_load, vil3d_save, etc add
00075 // the following code at the start of your program
00076 // \verbatim
00077 //  vil3d_file_format::add_format(new vimt3d_vil3d_v3m_format);
00078 // \endverbatim
00079 class vimt3d_vil3d_v3m_image: public vil3d_image_resource
00080 {
00081   friend class vimt3d_vil3d_v3m_format;
00082   //: Pointer to open image file.
00083   vcl_fstream *file_;
00084   //: Image cache.
00085   // Currently the whole image is cached im memory. This should be fixed.
00086   mutable vimt3d_image_3d *im_;
00087   //: If true, write image file on exit.
00088   bool dirty_;
00089 
00090   //: Private constructor, use vil3d_load instead.
00091   // This object takes ownership of the image.
00092   vimt3d_vil3d_v3m_image(vcl_auto_ptr<vcl_fstream> im);
00093   //: Private constructor, use vil3d_save instead.
00094   // This object takes ownership of the file, for writing.
00095   vimt3d_vil3d_v3m_image(vcl_auto_ptr<vcl_fstream> file, unsigned ni, unsigned nj,
00096                          unsigned nk, unsigned nplanes, vil_pixel_format format);
00097 
00098   //: Storage type for header information when the whole image has not yet been loaded.
00099   struct header_t
00100   {
00101     unsigned ni, nj, nk, nplanes;
00102     vimt3d_transform_3d w2i;
00103     header_t(): ni(0), nj(0), nk(0), nplanes(0) {}
00104     //: Expected pixel type.
00105     enum vil_pixel_format pixel_format;
00106     bool operator ==(const header_t& rhs) const;
00107   };
00108 
00109   //: Storage for header information when the whole image has not yet been loaded.
00110   mutable header_t header_;
00111 
00112   //: Load full image on demand.
00113   void load_full_image() const;
00114 
00115  public:
00116   virtual ~vimt3d_vil3d_v3m_image();
00117 
00118   //: Dimensions:  nplanes x ni x nj x nk.
00119   // This concept is treated as a synonym to components.
00120   virtual unsigned nplanes() const;
00121   //: Dimensions:  nplanes x ni x nj x nk.
00122   // The number of pixels in each row.
00123   virtual unsigned ni() const;
00124   //: Dimensions:  nplanes x ni x nj x nk.
00125   // The number of pixels in each column.
00126   virtual unsigned nj() const;
00127   //: Dimensions:  nplanes x ni x nj x nk.
00128   // The number of slices per image.
00129   virtual unsigned nk() const;
00130 
00131   //: Pixel Format.
00132   virtual enum vil_pixel_format pixel_format() const;
00133 
00134   //: Set the size of the each pixel in the i,j,k directions.
00135   // Return false if underlying image doesn't store pixel sizes.
00136   virtual bool set_voxel_size(float i, float j, float k);
00137 
00138   //: Get full world to image transform
00139   const vimt3d_transform_3d & world2im() const;
00140 
00141   //: Set full world to image transform
00142   // Call this before using put_view();
00143   void set_world2im(const vimt3d_transform_3d & tr);
00144 
00145   //: Create a read/write view of a copy of this data.
00146   // This function will always return a
00147   // multi-plane scalar-pixel view of the data.
00148   // \return 0 if unable to get view of correct size, or if resource is write-only.
00149   virtual vil3d_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
00150                                                    unsigned j0, unsigned nj,
00151                                                    unsigned k0, unsigned nk) const;
00152 
00153   //: Create a read/write view of a copy of this data.
00154   // This function will always return a
00155   // multi-plane scalar-pixel view of the data.
00156   // \return 0 if unable to get view of correct size, or if resource is write-only.
00157   virtual vil3d_image_view_base_sptr get_view(unsigned i0, unsigned ni,
00158                                               unsigned j0, unsigned nj,
00159                                               unsigned k0, unsigned nk) const;
00160 
00161   //: Put the data in this view back into the image source.
00162   // The view must be of scalar components. Assign your
00163   // view to a scalar-component view if this is not the case.
00164   // \return false if failed, because e.g. resource is read-only,
00165   // format of view is not correct (if it is a compound pixel type, try
00166   // assigning it to a multi-plane scalar pixel view.)
00167   virtual bool put_view(const vil3d_image_view_base& im,
00168                         unsigned i0, unsigned j0, unsigned k0);
00169 
00170   //: Return a string describing the file format.
00171   // Only file images have a format, others return 0
00172   virtual char const* file_format() const { return "v3m"; }
00173 
00174   //: Extra property information
00175   // This will just return the property of the first slice in the list.
00176   virtual bool get_property(char const* label, void* property_value = 0) const;
00177 };
00178 
00179 #endif