vil_pyramid_image_list.h
Go to the documentation of this file.
1 #ifndef vil_pyramid_image_list_h_
2 #define vil_pyramid_image_list_h_
3 //:
4 // \file
5 // \brief A pyramid image resource based on multiple file-based image resources
6 // \author J.L. Mundy March 20, 2006
7 // \verbatim
8 // Modifications:
9 // Nhon Trinh 07/11/2009 Changed definition of difference in scale to be log-based
10 // New definition: diff(a,b) = abs(log(a/b))
11 // \endverbatim
12 
13 #include <string>
14 #include <iostream>
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 #include <vil/vil_file_format.h>
20 
21 //The pyramid resource is made up of a set of image resources in
22 // a single directory - the image list.
24 {
25  public:
26  ~vil_pyramid_image_list_format() override = default;
27 
28  //: Return a character string which uniquely identifies this format.
29  //E.g. "pnm", "jpeg", etc.
30  char const* tag() const override {return "pyil";}//pyramid image list
31 
32  //: should return 0 so that no attempt is made to create a single image resource
34  { return nullptr; }
35 
36  //: Read a pyramid resource. Image list files are stored in directory.
38  make_input_pyramid_image(char const* directory) override;
39 
40 
41  //: Construct a pyramid image resource from a base image
42  // Each level has the same scale ratio (0.5) to the preceding level.
43  // Level 0 is the original base image. If copy base is false then
44  // Level 0 is already present in the directory and is used without
45  // modification. Each pyramid file in the directory is named
46  // filename + "level_index", e.g. R0, R1, ... Rn.
48  make_pyramid_image_from_base(char const* directory,
49  vil_image_resource_sptr const& base_image,
50  unsigned int nlevels,
51  bool copy_base,
52  char const* level_file_format,
53  char const* filename) override;
54 
55  //: A non-pyramid output image doesn't make sense here
57  unsigned int /*ni*/,
58  unsigned int /*nj*/,
59  unsigned int /*nplanes*/,
60  enum vil_pixel_format) override
61  { return nullptr; }
62 
64  make_pyramid_output_image(char const* directory) override;
65 };
66 
68 {
70  : scale_(1.0f), image_(image), cur_level_(0) {}
71  //: scale associated with level
72  float scale_;
73 
74  //:the resource
76 
77  //:the current pyramid level for this resource
78  unsigned int cur_level_;
79 
80  //:print ni and scale and values
81  void print(const unsigned int l)
82  {
83  std::cout << "level[" << l << "] scale: " << scale_
84  << " ni: " << image_->ni() << '\n';
85  }
86 };
87 
88 
90 {
91  public:
92  vil_pyramid_image_list(char const* directory);
93  vil_pyramid_image_list(std::vector<vil_image_resource_sptr> const& images);
94  ~vil_pyramid_image_list() override;
95  //: The number of planes (or components) in the image.
96  // This method refers to the base (max resolution) image
97  // Dimensions: Planes x ni x nj.
98  // This concept is treated as a synonym to components.
99  inline unsigned int nplanes() const override
100  {
101  if (levels_.size()>0)
102  return levels_[0]->image_->nplanes();
103  else
104  return 0;
105  }
106  //: The number of pixels in each row.
107  // This method refers to the base (max resolution) image
108  // Dimensions: Planes x ni x nj.
109  inline unsigned int ni() const override
110  {
111  if (levels_.size()>0)
112  return levels_[0]->image_->ni();
113  else
114  return 0;
115  }
116  //: The number of pixels in each column.
117  // This method refers to the base (max resolution) image
118  // Dimensions: Planes x ni x nj.
119  inline unsigned int nj() const override
120  {
121  if (levels_.size()>0)
122  return levels_[0]->image_->nj();
123  else
124  return 0;
125  }
126 
127  //: Pixel Format.
128 
129  inline enum vil_pixel_format pixel_format() const override
130  {
131  if (levels_.size()>0)
132  return levels_[0]->image_->pixel_format();
133  else
135  }
136 
137  //: Create a read/write view of a copy of this data.
138  // Applies only to the base image
139  inline vil_image_view_base_sptr get_copy_view(unsigned int i0,
140  unsigned int n_i,
141  unsigned int j0,
142  unsigned int n_j) const override
143  {
144  if (levels_.size()>0)
145  return levels_[0]->image_->get_copy_view(i0, n_i, j0, n_j);
146  else
147  return nullptr;
148  }
149 
150  //: Return a string describing the file format.
151  // Only file images have a format, others return 0
152  char const* file_format() const override { return "pryl"; }
153 
154  // --- Methods particular to pyramid resource ---
155 
156  //: number of levels in the pyramid
157  unsigned int nlevels() const override {return (unsigned int)(levels_.size());}
158 
159  //:Copy an image resource to the pyramid.
160  // If an image of the same scale already exists, then method returns false.
161  bool put_resource(vil_image_resource_sptr const& image) override;
162 
163  //:Add an image resource directly to the pyramid without copying.
164  // If an image of the same scale already exists, then method returns false.
165  bool add_resource(vil_image_resource_sptr const& image);
166 
167  //: virtual method for getting a level of the pyramid
168  vil_image_resource_sptr get_resource(const unsigned int level) const override
169  {return get_level(level);}
170 
171  //: Get a level image resource of the pyramid
172  inline vil_image_resource_sptr get_level(const unsigned int level) const
173  { if (level<levels_.size()) return levels_[level]->image_; else return nullptr; }
174 
175  //:Get a partial view from the image from a specified pyramid level
176  vil_image_view_base_sptr get_copy_view(unsigned int i0, unsigned int n_i,
177  unsigned int j0, unsigned int n_j,
178  unsigned int level) const override;
179 
180  //:Get a view from the image in the pyramid closest to scale
181  vil_image_view_base_sptr get_copy_view(const float scale, float& actual_scale) const override
182  { return get_copy_view(0, ni(), 0, nj(), scale, actual_scale); }
183 
184  //:Get a partial view from the image in the pyramid closest to scale.
185  // The origin and size parameters are in the coordinate system of the base image.
186  vil_image_view_base_sptr get_copy_view(unsigned int i0, unsigned int n_i,
187  unsigned int j0, unsigned int n_j,
188  const float scale,
189  float& actual_scale) const override;
190 
191  void set_directory(char const* directory) { directory_ = directory; }
192 
193  //for debugging purposes
194  void print(const unsigned int level) override
195  { if (level<levels_.size()) levels_[level]->print(level); }
196 
197  protected:
198  // no default constructor;
200 
201  // --- utility functions ---
202 
203  //:normalize the scale factors so that the base image scale = 1.0
204  void normalize_scales();
205 
206  //:find the image resource with scale closest to specified scale
207  pyramid_level* closest(const float scale) const;
208 
209  //:input image is the same size as one already in the pyramid
210  bool is_same_size(vil_image_resource_sptr const& image);
211 
212  //:find the nearest level to the image size
213  float find_next_level(vil_image_resource_sptr const& image);
214 
215  // --- members ---
216 
217  std::string directory_;
218 
219  //The set of images in the pyramid. levels_[0] is the base image
220  std::vector<pyramid_level*> levels_;
221 };
222 
223 #endif // vil_pyramid_image_list_h_
vil_pixel_format
Describes the type of the concrete data.
vil_image_resource_sptr make_input_image(vil_stream *) override
should return 0 so that no attempt is made to create a single image resource.
bool add_resource(vil_image_resource_sptr const &image)
Add an image resource directly to the pyramid without copying.
void print(const unsigned int l)
print ni and scale and values.
vil_pyramid_image_resource_sptr make_input_pyramid_image(char const *directory) override
Read a pyramid resource. Image list files are stored in directory.
vil_pyramid_image_resource_sptr make_pyramid_image_from_base(char const *directory, vil_image_resource_sptr const &base_image, unsigned int nlevels, bool copy_base, char const *level_file_format, char const *filename) override
Construct a pyramid image resource from a base image.
vil_image_resource_sptr image_
the resource.
unsigned int nplanes() const override
The number of planes (or components) in the image.
enum vil_pixel_format pixel_format() const override
Pixel Format.
Representation of a pyramid resolution hierarchy; mostly pure virtual methods.
Base class for image formats.
float scale_
scale associated with level.
void set_directory(char const *directory)
pyramid_level * closest(const float scale) const
find the image resource with scale closest to specified scale.
unsigned int nj() const override
The number of pixels in each column.
unsigned int cur_level_
the current pyramid level for this resource.
bool put_resource(vil_image_resource_sptr const &image) override
Copy an image resource to the pyramid.
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
char const * file_format() const override
Return a string describing the file format.
vil_pyramid_image_resource_sptr make_pyramid_output_image(char const *directory) override
bool is_same_size(vil_image_resource_sptr const &image)
input image is the same size as one already in the pyramid.
pyramid_level(vil_image_resource_sptr const &image)
Representation of a pyramid resolution hierarchy.
vil_image_resource_sptr make_output_image(vil_stream *, unsigned int, unsigned int, unsigned int, enum vil_pixel_format) override
A non-pyramid output image doesn't make sense here.
~vil_pyramid_image_list_format() override=default
float find_next_level(vil_image_resource_sptr const &image)
find the nearest level to the image size.
void normalize_scales()
normalize the scale factors so that the base image scale = 1.0.
vil_image_view_base_sptr get_copy_view() const
Create a read/write view of a copy of all the data.
unsigned int nlevels() const override
number of levels in the pyramid.
vil_image_resource_sptr get_resource(const unsigned int level) const override
virtual method for getting a level of the pyramid.
vil_image_view_base_sptr get_copy_view(unsigned int i0, unsigned int n_i, unsigned int j0, unsigned int n_j) const override
Create a read/write view of a copy of this data.
vil_image_resource_sptr get_level(const unsigned int level) const
Get a level image resource of the pyramid.
char const * tag() const override
Return a character string which uniquely identifies this format.
Base class for image formats.
unsigned int ni() const override
The number of pixels in each row.
void print(const unsigned int level) override
std::vector< pyramid_level * > levels_
vil_image_view_base_sptr get_copy_view(const float scale, float &actual_scale) const override
Get a view from the image in the pyramid closest to scale.