core/vil/file_formats/vil_pyramid_image_list.h
Go to the documentation of this file.
00001 #ifndef vil_pyramid_image_list_h_
00002 #define vil_pyramid_image_list_h_
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma interface
00005 #endif
00006 //:
00007 // \file
00008 // \brief A pyramid image resource based on multiple file-based image resources
00009 // \author J.L. Mundy March 20, 2006
00010 // \verbatim
00011 //  Modifications:
00012 //   Nhon Trinh  07/11/2009  Changed definition of difference in scale to be log-based
00013 //                           New definition: diff(a,b) = abs(log(a/b))
00014 // \endverbatim
00015 
00016 #include <vcl_string.h>
00017 #include <vcl_iostream.h>
00018 #include <vil/vil_file_format.h>
00019 #include <vil/vil_pyramid_image_resource.h>
00020 
00021 //The pyramid resource is made up of a set of image resources in
00022 // a single directory - the image list.
00023 class vil_pyramid_image_list_format : public vil_file_format
00024 {
00025  public:
00026   ~vil_pyramid_image_list_format(){}
00027 
00028   //: Return a character string which uniquely identifies this format.
00029   //E.g. "pnm", "jpeg", etc.
00030   virtual char const* tag() const {return "pyil";}//pyramid image list
00031 
00032   //: should return 0 so that no attempt is made to create a single image resource
00033   virtual vil_image_resource_sptr make_input_image(vil_stream* /*vs*/)
00034   { return 0; }
00035 
00036   //: Read a pyramid resource. Image list files are stored in directory.
00037   virtual vil_pyramid_image_resource_sptr
00038   make_input_pyramid_image(char const* directory);
00039 
00040 
00041   //: Construct a pyramid image resource from a base image
00042   //  Each level has the same scale ratio (0.5) to the preceding level.
00043   //  Level 0 is the original base image. If copy base is false then
00044   //  Level 0 is already present in the directory and is used without
00045   //  modification. Each pyramid file in the directory is named
00046   //  filename + "level_index", e.g. R0, R1, ... Rn.
00047   virtual vil_pyramid_image_resource_sptr
00048   make_pyramid_image_from_base(char const* directory,
00049                                vil_image_resource_sptr const& base_image,
00050                                unsigned int nlevels,
00051                                bool copy_base,
00052                                char const* level_file_format,
00053                                char const* filename);
00054 
00055   //: A non-pyramid output image doesn't make sense here
00056   virtual vil_image_resource_sptr make_output_image(vil_stream* /*vs*/,
00057                                                     unsigned int /*ni*/,
00058                                                     unsigned int /*nj*/,
00059                                                     unsigned int /*nplanes*/,
00060                                                     enum vil_pixel_format)
00061   { return 0; }
00062 
00063   virtual vil_pyramid_image_resource_sptr
00064     make_pyramid_output_image(char const* directory);
00065 };
00066 
00067 struct pyramid_level
00068 {
00069   pyramid_level(vil_image_resource_sptr const& image)
00070   : scale_(1.0f), image_(image), cur_level_(0) {}
00071   //: scale associated with level
00072   float scale_;
00073 
00074   //:the resource
00075   vil_image_resource_sptr image_;
00076 
00077   //:the current pyramid level for this resource
00078   unsigned int cur_level_;
00079 
00080   //:print ni and scale and values
00081   void print(const unsigned int l)
00082   {
00083     vcl_cout << "level[" << l <<  "]  scale: " << scale_
00084              << "  ni: " << image_->ni() << '\n';
00085   }
00086 };
00087 
00088 
00089 class vil_pyramid_image_list : public vil_pyramid_image_resource
00090 {
00091  public:
00092   vil_pyramid_image_list(char const* directory);
00093   vil_pyramid_image_list(vcl_vector<vil_image_resource_sptr> const& images);
00094   virtual ~vil_pyramid_image_list();
00095   //: The number of planes (or components) in the image.
00096   // This method refers to the base (max resolution) image
00097   // Dimensions:  Planes x ni x nj.
00098   // This concept is treated as a synonym to components.
00099   inline virtual unsigned int nplanes() const
00100   {
00101     if (levels_.size()>0)
00102       return levels_[0]->image_->nplanes();
00103     else
00104       return 0;
00105   }
00106   //: The number of pixels in each row.
00107   // This method refers to the base (max resolution) image
00108   // Dimensions:  Planes x ni x nj.
00109   inline virtual unsigned int ni() const
00110   {
00111     if (levels_.size()>0)
00112       return levels_[0]->image_->ni();
00113     else
00114       return 0;
00115   }
00116   //: The number of pixels in each column.
00117   // This method refers to the base (max resolution) image
00118   // Dimensions:  Planes x ni x nj.
00119   inline virtual unsigned int nj() const
00120   {
00121     if (levels_.size()>0)
00122       return levels_[0]->image_->nj();
00123     else
00124       return 0;
00125   }
00126 
00127   //: Pixel Format.
00128 
00129   inline virtual enum vil_pixel_format pixel_format() const
00130   {
00131     if (levels_.size()>0)
00132       return levels_[0]->image_->pixel_format();
00133     else
00134       return VIL_PIXEL_FORMAT_UNKNOWN;
00135   }
00136 
00137   //: Create a read/write view of a copy of this data.
00138   // Applies only to the base image
00139   inline virtual vil_image_view_base_sptr get_copy_view(unsigned int i0,
00140                                                         unsigned int n_i,
00141                                                         unsigned int j0,
00142                                                         unsigned int n_j) const
00143   {
00144     if (levels_.size()>0)
00145       return levels_[0]->image_->get_copy_view(i0, n_i, j0, n_j);
00146     else
00147       return 0;
00148   }
00149 
00150   //: Return a string describing the file format.
00151   // Only file images have a format, others return 0
00152   virtual char const* file_format() const { return "pryl"; }
00153 
00154         // --- Methods particular to pyramid resource ---
00155 
00156   //: number of levels in the pyramid
00157   virtual unsigned int nlevels() const {return (unsigned int)(levels_.size());}
00158 
00159   //:Copy an image resource to the pyramid.
00160   // If an image of the same scale already exists, then method returns false.
00161   bool put_resource(vil_image_resource_sptr const& image);
00162 
00163   //:Add an image resource directly to the pyramid without copying.
00164   // If an image of the same scale already exists, then method returns false.
00165   bool add_resource(vil_image_resource_sptr const& image);
00166 
00167   //: virtual method for getting a level of the pyramid
00168   vil_image_resource_sptr get_resource(const unsigned int level) const
00169     {return get_level(level);}
00170 
00171   //: Get a level image resource of the pyramid
00172   inline vil_image_resource_sptr get_level(const unsigned int level) const
00173   { if (level<levels_.size()) return levels_[level]->image_; else return 0; }
00174 
00175   //:Get a partial view from the image from a specified pyramid level
00176   virtual vil_image_view_base_sptr get_copy_view(unsigned int i0, unsigned int n_i,
00177                                                  unsigned int j0, unsigned int n_j,
00178                                                  unsigned int level) const;
00179 
00180   //:Get a view from the image in the pyramid closest to scale
00181   vil_image_view_base_sptr get_copy_view(const float scale, float& actual_scale) const
00182   { return get_copy_view(0, ni(), 0, nj(), scale, actual_scale); }
00183 
00184   //:Get a partial view from the image in the pyramid closest to scale.
00185   // The origin and size parameters are in the coordinate system of the base image.
00186   vil_image_view_base_sptr get_copy_view(unsigned int i0, unsigned int n_i,
00187                                          unsigned int j0, unsigned int n_j,
00188                                          const float scale,
00189                                          float& actual_scale) const;
00190 
00191   void set_directory(char const* directory) { directory_ = directory; }
00192 
00193   //for debugging purposes
00194   void print(const unsigned int level)
00195   { if (level<levels_.size()) levels_[level]->print(level); }
00196 
00197  protected:
00198   // no default constructor;
00199   vil_pyramid_image_list();
00200 
00201            //    --- utility functions ---
00202 
00203   //:normalize the scale factors so that the base image scale = 1.0
00204   void normalize_scales();
00205 
00206   //:find the image resource with scale closest to specified scale
00207   pyramid_level* closest(const float scale) const;
00208 
00209   //:input image is the same size as one already in the pyramid
00210   bool is_same_size(vil_image_resource_sptr const& image);
00211 
00212   //:find the nearest level to the image size
00213   float find_next_level(vil_image_resource_sptr const& image);
00214 
00215           //    ---  members ---
00216 
00217   vcl_string directory_;
00218 
00219   //The set of images in the pyramid. levels_[0] is the base image
00220   vcl_vector<pyramid_level*> levels_;
00221 };
00222 
00223 #endif // vil_pyramid_image_list_h_