vil_j2k_pyramid_image_resource.cxx
Go to the documentation of this file.
1 #include <cmath>
3 //:
4 // \file
5 // Do not remove the following notice
6 // Approved for public Release, distribution unlimited
7 // DISTAR Case 14074
8 
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 
13 // By definition, each level is a factor of 2 reduced in scale
14 static float scale_at_level(unsigned level)
15 {
16  if (level == 0)
17  return 1.0f;
18  float s = std::pow(2.0f, -static_cast<float>(level));
19  return s;
20 }
21 
24 {
25  ptr_ = 0;
26  if (!j2k_sptr_)
27  return;
28  std::string fmt = j2k_sptr_->file_format();
29  if (fmt=="j2k")
30  ptr_=static_cast<vil_j2k_image*>(j2k_sptr_.ptr());
31 }
32 
34 {
35  unsigned ret = 0;
36  if (ptr_)
37  ret = ptr_->nplanes();
38  return ret;
39 }
40 
41  //: The number of pixels in each row.
42  // Dimensions: Planes x ni x nj.
43  // This method refers to the base (max resolution) image
45 {
46  unsigned ret = 0;
47  if (ptr_)
48  ret = ptr_->ni();
49  return ret;
50 }
51 
52 //: The number of pixels in each column.
53 // Dimensions: Planes x ni x nj.
54 // This method refers to the base (max resolution) image
56 {
57  unsigned ret = 0;
58  if (ptr_)
59  ret = ptr_->nj();
60  return ret;
61 }
62 
63 //: Pixel Format.
65 {
66  if (ptr_)
67  return ptr_->pixel_format();
69 }
70 
71 //: Return a string describing the file format.
72  // Only file images have a format, others return 0
74 {
75  return "j2k_pyramid";
76 }
77 
78 
79  // === Methods particular to pyramid resource ===
80 
81 //: Number of pyramid levels.
82 // Defined to assign 1000 pixels to the largest
83 // dimension of the least resolution level
85 {
86  if (!ptr_)
87  return 0;
88  unsigned max_dim = this->ni(), nj = this->nj();
89  if (nj>max_dim)
90  max_dim = nj;
91  if (max_dim<1000)
92  return 1;
93  // Assume top level of the pyramid has maximum dimension of 1K pixels
94  double scale = max_dim/1000.0;
95  if (scale<=1.0)
96  return 1;
97  double lscale = std::log(scale);
98  unsigned nlev = static_cast<unsigned>(lscale/std::log(2.0));
99  return nlev;
100 }
101 
102 //: Get a partial view from the image from a specified pyramid level
105  unsigned j0, unsigned n_j,
106  unsigned level) const
107 {
108  if (!ptr_||!(ptr_->is_valid()))
109  return 0;
110  if (level>this->nlevels())
111  level = this->nlevels()-1;
112  double s = scale_at_level(level);
113  double factor = static_cast<unsigned>(1/s);
114  return ptr_->get_copy_view_decimated(i0, n_i, j0, n_j, factor, factor);
115 }
116 
117 //: Get a partial view from the image in the pyramid closest to scale.
118 // The origin and size parameters are in the coordinate system of the base image.
119 // The scale factor is with respect to the base image (base scale = 1.0).
122  unsigned j0, unsigned n_j,
123  const float scale,
124  float& actual_scale) const
125 {
126  if (scale>=1.0f)
127  {
128  actual_scale = 1.0f;
129  return this->get_copy_view(i0, n_i, j0, n_j, 0);
130  }
131  float f_lev = -std::log(scale)/std::log(2.0f);
132  unsigned level = static_cast<unsigned>(f_lev);
133  if (level>this->nlevels())
134  level = this->nlevels()-1;
135  actual_scale = scale_at_level(level);
136  return this->get_copy_view(i0, n_i, j0, n_j, level);
137 }
138 
139 //: Get an image resource from the pyramid at the specified level
142 {
143  if (level==0)
144  return j2k_sptr_;
145  return 0;
146 }
147 
148 //: for debug purposes
149 void vil_j2k_pyramid_image_resource::print(const unsigned level)
150 {
151 }
vil_pixel_format
Describes the type of the concrete data.
virtual unsigned nj() const
Dimensions: Planes x ni x nj.
virtual unsigned nj() const
The number of pixels in each column.
virtual unsigned ni() const
The number of pixels in each row.
bool is_valid() const
Call this after construction to see if you can get valid data from me.
virtual unsigned nplanes() const
Dimensions: planes x width x height x components.
virtual unsigned ni() const
Dimensions: Planes x ni x nj.
virtual char const * file_format() const
Return a string describing the file format.
virtual unsigned nlevels() const
Number of pyramid levels.
virtual vil_image_resource_sptr get_resource(const unsigned level) const
Get an image resource from the pyramid at the specified level.
vil_image_view_base_sptr get_copy_view() const
Create a read/write view of a copy of all the data.
virtual enum vil_pixel_format pixel_format() const
Pixel Format.
virtual vil_image_view_base_sptr get_copy_view_decimated(unsigned i0, unsigned ni, unsigned j0, unsigned nj, double i_factor, double j_factor) const
virtual unsigned nplanes() const
The number of planes (or components) in the image.
virtual enum vil_pixel_format pixel_format() const
Pixel Format.
Representation of a pyramid resolution hierarchy based on the j2k_image.
T * ptr() const
These methods all return the raw/dumb pointer.
virtual void print(const unsigned level)
for debug purposes.