contrib/mul/vimt/vimt_vil_v2i.h
Go to the documentation of this file.
00001 // This is mul/vimt/vimt_vil_v2i.h
00002 #ifndef vimt_vil_v2i_h_
00003 #define vimt_vil_v2i_h_
00004 
00005 //:
00006 // \file
00007 // \brief Reader/Writer for v2i format images.
00008 // \author Ian Scott - Manchester
00009 // v2i is a VXL specific format designed to store all the information that
00010 // can be stored in a vimt image. We don't recommend its use in general,
00011 // having no wish to add to the plethora of badly designed image formats
00012 // out there. However, a means of being able to save, and reload a full
00013 // vil/vimt image using the standard vil_load_* API is very useful.
00014 // The file format currently is that provided by the default vsl
00015 // serialisation scheme. There is an extra v2i-specific magic number to
00016 // reduce confusion with other vsl data files, and a version number to allow
00017 // for a change in format.
00018 
00019 
00020 #include <vil/vil_file_format.h>
00021 #include <vimt/vimt_image_2d.h>
00022 // not used? #include <vcl_iosfwd.h>
00023 
00024 
00025 //: Reader/Writer for v2i format images.
00026 //
00027 // To add this plugin to the list of loaders either
00028 // \verbatim
00029 //   vil_file_format::add_file_format(new vimt_vil_v2i_format);
00030 // \endverbatim
00031 // or call vimt_add_all_binary_loaders()
00032 class vimt_vil_v2i_format: public vil_file_format
00033 {
00034  public:
00035   vimt_vil_v2i_format() {}
00036   //: The destructor must be virtual so that the memory chunk is destroyed.
00037   virtual ~vimt_vil_v2i_format() {}
00038 
00039   virtual vil_image_resource_sptr make_input_image(vil_stream* vs);
00040 
00041   //: Make a "generic_image" on which put_section may be applied.
00042   // The file may be opened immediately for writing so that a header can be written.
00043   virtual vil_image_resource_sptr make_output_image(vil_stream* vs,
00044                                                     unsigned ni,
00045                                                     unsigned nj,
00046                                                     unsigned nplanes,
00047                                                     enum vil_pixel_format);
00048 
00049   //: default filename tag for this image.
00050   virtual const char * tag() const {return "v2i";}
00051 };
00052 
00053 
00054 // You can't create one of these yourself.
00055 // Use vimt_vil_v2i_format instead.
00056 class vimt_vil_v2i_image: public vil_image_resource
00057 {
00058   friend class vimt_vil_v2i_format;
00059   //: Pointer to open image file.
00060   vil_stream *vs_;
00061   //: Image cache.
00062   // Currently the whole image is cached im memory. This should be fixed.
00063   vimt_image_2d *im_;
00064   //: If true, write image file on exit.
00065   bool dirty_;
00066 
00067   //: Expected pixel type.
00068   enum vil_pixel_format pixel_format_;
00069   //: Private constructor, use vil_load instead.
00070   // This object takes ownership of the file.
00071   vimt_vil_v2i_image(vil_stream * vs);
00072   //: Private constructor, use vil_load instead.
00073   // This object takes ownership of the file.
00074   vimt_vil_v2i_image(vil_stream * vs, vil_pixel_format f);
00075   //: Private constructor, use vil_save instead.
00076   // This object takes ownership of the file, for writing.
00077   vimt_vil_v2i_image(vil_stream* vs, unsigned ni, unsigned nj,
00078                      unsigned nplanes, vil_pixel_format format);
00079 
00080  public:
00081   virtual ~vimt_vil_v2i_image();
00082 
00083   //: Dimensions:  nplanes x ni x nj.
00084   // This concept is treated as a synonym to components.
00085   virtual unsigned nplanes() const;
00086   //: Dimensions:  nplanes x ni x nj.
00087   // The number of pixels in each row.
00088   virtual unsigned ni() const;
00089   //: Dimensions:  nplanes x ni x nj.
00090   // The number of pixels in each column.
00091   virtual unsigned nj() const;
00092 
00093   //: Pixel Format.
00094   virtual enum vil_pixel_format pixel_format() const;
00095 
00096   //: Set the size of the each pixel in the i,j directions.
00097   void set_pixel_size(float i, float j);
00098 
00099   //: Get full world to image transform
00100   const vimt_transform_2d & world2im() const;
00101 
00102   //: Set full world to image transform
00103   // Call this before using put_view();
00104   void set_world2im(const vimt_transform_2d & tr);
00105 
00106   //: Create a read/write view of a copy of this data.
00107   // This function will always return a
00108   // multi-plane scalar-pixel view of the data.
00109   // \return 0 if unable to get view of correct size, or if resource is write-only.
00110   virtual vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
00111                                                  unsigned j0, unsigned nj) const;
00112 
00113   //: Create a read/write view of a copy of this data.
00114   // This function will always return a
00115   // multi-plane scalar-pixel view of the data.
00116   // \return 0 if unable to get view of correct size, or if resource is write-only.
00117   virtual vil_image_view_base_sptr get_view(unsigned i0, unsigned ni,
00118                                             unsigned j0, unsigned nj) const;
00119 
00120   //: Put the data in this view back into the image source.
00121   // The view must be of scalar components. Assign your
00122   // view to a scalar-component view if this is not the case.
00123   // \return false if failed, because e.g. resource is read-only,
00124   // format of view is not correct (if it is a compound pixel type, try
00125   // assigning it to a multi-plane scalar pixel view.)
00126   virtual bool put_view(const vil_image_view_base& im,
00127                         unsigned i0, unsigned j0);
00128 
00129   //: Return a string describing the file format.
00130   // Only file images have a format, others return 0
00131   virtual char const* file_format() const { return "v2i"; }
00132 
00133   //: Extra property information
00134   // This will just return the property of the first slice in the list.
00135   virtual bool get_property(char const* label, void* property_value = 0) const;
00136 };
00137 
00138 #endif