vil_blocked_image_facade.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_blocked_image_facade.cxx
2 #include <cstring>
4 
5 #ifdef _MSC_VER
6 # include <vcl_msvc_warnings.h>
7 #endif
8 #include <cassert>
9 #include <vil/vil_property.h>
10 #include <vil/vil_image_view.h>
11 #include <vil/vil_crop.h>
12 #include <vil/vil_copy.h>
13 
14 //#define DEBUG
15 
16 static const unsigned vil_size_block_i = 256, vil_size_block_j = 256;
17 
18 vil_blocked_image_facade::vil_blocked_image_facade(const vil_image_resource_sptr &src, const unsigned sbi, const unsigned sbj):
19  src_(src)
20 {
21  //cases
22  // I the blocking is specified so use it
23  if (sbi>0&&sbj>0)
24  {
25  sbi_ = sbi; sbj_=sbj;
26  return;
27  }
28  // II Use the default block size
29  sbi_ = vil_size_block_i; sbj_ = vil_size_block_j;
30  //set up the buffer
31 }
32 
35 {
37  {
38 #define FILL_BLOCK_CASE(FORMAT, T) \
39  case FORMAT: { \
40  vil_image_view<T>* dest = new vil_image_view<T>(sbi_, sbj_, nplanes()); \
41  vil_image_view_base_sptr ptr = dest; \
42  vil_image_view<T>* src = reinterpret_cast<vil_image_view<T>* >(view.ptr()); \
43  vil_copy_to_window<T>(*src, *dest, 0, 0); \
44  return dest; \
45  } break
55 #undef FILL_BLOCK_CASE
56  default:
57  assert(!"Unknown vil data type.");
58  return nullptr;
59  }
60  return nullptr;
61 }
62 
63 //Get a view that is the size of a block. If the block is on the
64 //border then the block pixels outside the image boundary are undefined
66 vil_blocked_image_facade::get_block( unsigned block_index_i,
67  unsigned block_index_j ) const
68 {
69  unsigned ni = src_->ni(), nj = src_->nj();
70  unsigned i0 = block_index_i*sbi_, j0 = block_index_j*sbj_;
71  if (i0>ni-1||j0>nj-1) return nullptr;
72  //check if the view that is supplied is smaller than a block
73  unsigned icrop = ni-i0, jcrop = nj-j0;
74  bool needs_fill = false;
75  if (icrop>=sbi_)
76  icrop = sbi_;
77  else
78  needs_fill = true;
79 
80  if (jcrop>=sbj_)
81  jcrop = sbj_;
82  else
83  needs_fill = true;
84  vil_image_view_base_sptr view = src_->get_view(i0, icrop, j0, jcrop);
85  if (needs_fill)
86  view = fill_block(view);
87  return view;
88 }
89 
90 bool vil_blocked_image_facade::put_block(unsigned block_index_i,
91  unsigned block_index_j,
92  const vil_image_view_base& view)
93 {
94  // convert to image coordinates
95  unsigned i0 = block_index_i*sbi_, j0 = block_index_j*sbj_;
96  // check if block is too big for the destination
97  unsigned imax = i0 + sbi_, jmax = j0 + sbj_;
98  unsigned icrop = sbi_, jcrop = sbj_;
99  bool needs_trim = false;
100  if (imax>src_->ni())
101  {
102  icrop = src_->ni()-i0;
103  needs_trim = true;
104  }
105  if (jmax>src_->nj())
106  {
107  jcrop = src_->nj()-j0;
108  needs_trim = true;
109  }
110 
111  if (needs_trim)
113  {
114 #define TRIM_BLOCK_CASE(FORMAT, T) \
115  case FORMAT: { \
116  const vil_image_view<T>& curr_view = \
117  static_cast<const vil_image_view< T >& >(view); \
118  vil_image_view< T > cview = \
119  vil_crop(curr_view, 0, icrop, 0, jcrop); \
120  return src_->put_view(cview, i0, j0); \
121  } break
131 #undef TRIM_BLOCK_CASE
132  default:
133  assert(!"Unknown vil data type.");
134  return false;
135  }
136  // no trim was required
137 return src_->put_view(view, i0, j0);
138 }
139 
140 
141 bool vil_blocked_image_facade::get_property(char const * tag, void * value) const
142 {
143  if (std::strcmp(vil_property_quantisation_depth, tag)==0)
144  return src_->get_property(tag, value);
145 
146  if (std::strcmp(vil_property_size_block_i, tag)==0)
147  {
148  if (value)
149  *static_cast<unsigned*>(value) = this->size_block_i();
150  return true;
151  }
152 
153  if (std::strcmp(vil_property_size_block_j, tag)==0)
154  {
155  if (value)
156  *static_cast<unsigned*>(value) = this->size_block_j();
157  return true;
158  }
159 
160  return false;
161 }
An abstract base class of smart pointers to actual image data in memory.
vil_pixel_format vil_pixel_format_component_format(enum vil_pixel_format f)
Return the number of components in pixel format f.
vil_image_view_base_sptr get_block(unsigned block_index_i, unsigned block_index_j) const override
Block access.
unsigned ni() const override
Dimensions: Planes x ni x nj.
bool get_property(char const *tag, void *property_value=nullptr) const override
Extra property information.
#define vil_property_quantisation_depth
The quantisation depth of pixel components.
Definition: vil_property.h:67
vil_image_resource_sptr src_
the image resource masquerading as a blocked resource.
bool put_block(unsigned block_index_i, unsigned block_index_j, const vil_image_view_base &view) override
put the block into the resource at the indicated location.
unsigned size_block_i() const override
Block size in columns.
#define TRIM_BLOCK_CASE(FORMAT, T)
A blocked image facade for any image resource.
#define vil_property_size_block_i
For unblocked images, the following properties are not implemented.
Definition: vil_property.h:76
Various image copying functions.
#define FILL_BLOCK_CASE(FORMAT, T)
unsigned size_block_j() const override
Block size in rows.
vil_image_view_base_sptr fill_block(vil_image_view_base_sptr &view) const
A base class reference-counting view of some image data.
There is no class or function called vil_property.
enum vil_pixel_format pixel_format() const override
Pixel Format.
#define vil_property_size_block_j
Block size in rows.
Definition: vil_property.h:79
unsigned sbj_
block size in j.
unsigned sbi_
block size in i.
unsigned nj() const override
Dimensions: Planes x ni x nj.