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