[20a6d9]: core / vil / vil_image_view.h  Maximize  Restore  History

Download this file

326 lines (266 with data), 13.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// This is core/vil/vil_image_view.h
#ifndef vil_image_view_h_
#define vil_image_view_h_
#ifdef VCL_NEEDS_PRAGMA_INTERFACE
#pragma interface
#endif
//:
// \file
// \brief A base class reference-counting view of some image data.
// \author Ian Scott - Manchester
// \verbatim
// Modifications
// Peter Vanroose - 21 Aug.2003 - support added for interleaved planes
// \endverbatim
#include <vcl_iosfwd.h>
#include <vcl_string.h>
#include <vcl_cassert.h>
#include <vcl_cstddef.h>
#include <vil/vil_image_view_base.h>
#include <vil/vil_memory_chunk.h>
#include <vil/vil_pixel_format.h>
//: Concrete view of image data of type T held in memory
// Views nplanes() planes of data each of size ni() x nj().
// The (i,j) element of the p'th plane is given by
// im.top_left_ptr()[i*im.istep() + j*im.jstep() + p*im.planestep]
// The actual image data is either allocated by the class
// (using set_size), in which case it is deleted
// only when it has no views observing it, or is allocated outside (and is not
// deleted on destruction). This allows external images to be accessed
// without a deep copy.
//
// Note that copying one vil_image_view<T> to another takes a shallow
// copy by default - it copies the view, not the raw image data.
// Use the explicit deep_copy() call to take a deep copy.
template <class T>
class vil_image_view : public vil_image_view_base
{
private:
VCL_SAFE_BOOL_DEFINE;
protected:
//: Pointer to pixel at origin.
T * top_left_;
//: Add this to a pixel pointer to move one column left.
vcl_ptrdiff_t istep_;
//: Add this to a pixel pointer to move one row down.
vcl_ptrdiff_t jstep_;
//: Add this to a pixel pointer to move one plane back.
vcl_ptrdiff_t planestep_;
//: Reference to actual image data.
vil_memory_chunk_sptr ptr_;
//: Disconnect this view from the underlying data,
void release_memory() { ptr_ = 0; }
public:
//: Dflt ctor
// Creates an empty one-plane image.
vil_image_view(): top_left_(0),istep_(0),jstep_(0),planestep_(0) {}
//: Create an image of ni x nj pixels in (n_planes * n_interleaved_planes) planes
// If n_interleaved_planes > 1, the planes are interleaved.
// If n_planes > 1, each plane of pixels is stored contiguously.
// n_planes and n_components should not be both different from 1.
// n_planes * n_interleaved_planes should be 1 unless T is scalar.
vil_image_view(unsigned ni, unsigned nj, unsigned n_planes=1, unsigned n_interleaved_planes=1);
//: Set this view to look at someone else's memory data.
// If the data goes out of scope then this view could be invalid, and
// there's no way of knowing until its too late - so take care!
vil_image_view(const T* top_left, unsigned ni, unsigned nj, unsigned nplanes,
vcl_ptrdiff_t i_step, vcl_ptrdiff_t j_step, vcl_ptrdiff_t plane_step);
//: Set this view to look at another view's data
// Typically used by functions which generate a manipulated view of
// another's image data.
// Need to pass the memory chunk to set up the internal smart ptr appropriately
vil_image_view(const vil_memory_chunk_sptr& mem_chunk,
const T* top_left, unsigned ni, unsigned nj, unsigned nplanes,
vcl_ptrdiff_t i_step, vcl_ptrdiff_t j_step, vcl_ptrdiff_t plane_step);
//: Copy construct.
// The new object will point to the same underlying image as the rhs.
vil_image_view(const vil_image_view<T>& rhs);
//: Construct from various vil_image_view types.
// The new object will point to the same underlying image as the rhs
// You can assign a vil_image_view<compound_type<T>> to a vil_image_view<T>
// in all reasonable cases - the lhs will have as many planes as the rhs has
// components. You can assign a vil_image_view<T> to a vil_image_view<compound_type<T>>
// when the underlying data is formatted appropriately and the lhs has
// as many components as the rhs has planes. O(1).
// If the view types are not compatible this object will be set to empty.
vil_image_view(const vil_image_view_base& rhs);
//: Construct from various vil_image_view types.
// The new object will point to the same underlying image as the rhs.
//
// You can assign a vil_image_view<compound_type<T>> to a vil_image_view<T>
// in all reasonable cases - the lhs will have as many planes as the rhs has
// components. You can assign a vil_image_view<T> to a vil_image_view<compound_type<T>>
// when the underlying data is formatted appropriately and the lhs has
// as many components as the rhs has planes. O(1).
// \throws vil_exception_pixel_formats_incompatible if view types are not compatible. Or
// returns a null image if exceptions are disabled.
vil_image_view(const vil_image_view_base_sptr& rhs);
// Destructor
virtual ~vil_image_view() {}
// Standard container stuff
// This assumes that the data is arranged contiguously.
// Is this assumption good?
//: The pixel type of this image
typedef T pixel_type;
//: True if data all in one unbroken block and top_left_ptr() is lowest data address
bool is_contiguous() const;
// iterators
typedef T *iterator;
inline iterator begin() { assert(is_contiguous()); return top_left_; }
inline iterator end () { assert(is_contiguous()); return top_left_ + size(); }
typedef T const *const_iterator;
inline const_iterator begin() const { assert(is_contiguous()); return top_left_; }
inline const_iterator end () const { assert(is_contiguous()); return top_left_ + size(); }
// arithmetic indexing stuff
//: Pointer to the first (top left in plane 0) pixel.
// Note that this is not necessarily the lowest data memory address.
inline T * top_left_ptr() { return top_left_; } // Make origin explicit
//: Pointer to the first (top left in plane 0) pixel.
// Note that this is not necessarily the lowest data memory address.
inline const T * top_left_ptr() const { return top_left_; }
//: Add this to your pixel pointer to get next i pixel.
// Note that istep() may well be negative; see e.g. vil_flip_lr
inline vcl_ptrdiff_t istep() const { return istep_; }
//: Add this to your pixel pointer to get next j pixel.
// Note that jstep() may well be negative; see e.g. vil_flip_ud
inline vcl_ptrdiff_t jstep() const { return jstep_; }
//: Add this to your pixel pointer to get pixel on next plane.
// Note that planestep() may well be negative, e.g. with BMP file images
inline vcl_ptrdiff_t planestep() const { return planestep_; }
//: Cast to bool is true if pointing at some data.
operator safe_bool () const
{ return (top_left_ != (T*)0)? VCL_SAFE_BOOL_TRUE : 0; }
//: Return false if pointing at some data.
bool operator!() const
{ return (top_left_ != (T*)0)? false : true; }
//: The number of bytes in the data
inline unsigned size_bytes() const { return size() * sizeof(T); }
//: Smart pointer to the object holding the data for this view
// Will be a null pointer if this view looks at `third-party' data,
// e.g. using set_to_memory.
//
// Typically used when creating new views of the data
inline const vil_memory_chunk_sptr& memory_chunk() const { return ptr_; }
//: Smart pointer to the object holding the data for this view
// Will be a null pointer if this view looks at `third-party' data,
// e.g. using set_to_memory
//
// Typically used when creating new views of the data
inline vil_memory_chunk_sptr& memory_chunk() { return ptr_; }
// Ordinary image indexing stuff.
//: Return true if (i,j) is a valid index into this buffer.
inline bool in_range (int i, int j) const
{ return (i>-1) && (i<(int)ni_) && (j>-1) && (j<(int)nj_); }
//: Return true if (i,j,p) is a valid index into this buffer.
inline bool in_range (int i, int j, int p) const
{ return (i>-1) && (i<(int)ni_) && (j>-1) && (j<(int)nj_)
&& (p>-1) && (p<(int)nplanes_); }
//: Return read-only reference to pixel at (i,j) in plane 0.
inline const T& operator()(unsigned i, unsigned j) const {
assert(i<ni_); assert(j<nj_);
return top_left_[jstep_*j+i*istep_]; }
//: Return read/write reference to pixel at (i,j) in plane 0.
inline T& operator()(unsigned i, unsigned j) {
assert(i<ni_); assert(j<nj_);
return top_left_[istep_*i+j*jstep_]; }
//: Return read-only reference to pixel at (i,j) in plane p.
inline const T& operator()(unsigned i, unsigned j, unsigned p) const {
assert(i<ni_); assert(j<nj_); assert(p<nplanes_);
return top_left_[p*planestep_ + j*jstep_ + i*istep_]; }
//: Return read-only reference to pixel at (i,j) in plane p.
inline T& operator()(unsigned i, unsigned j, unsigned p) {
assert(i<ni_); assert(j<nj_); assert(p<nplanes_);
return top_left_[p*planestep_ + j*jstep_ + i*istep_]; }
// image stuff
//: resize current planes to ni x nj
// If already correct size, this function returns quickly
virtual void set_size(unsigned ni, unsigned nj);
//: resize to ni x nj x nplanes
// If already correct size, this function returns quickly
virtual void set_size(unsigned ni, unsigned nj, unsigned nplanes);
//: Make a copy of the data in src and set this to view it
void deep_copy(const vil_image_view<T>& src);
//: Make empty.
// Disconnects view from underlying data.
inline void clear() { release_memory(); ni_=nj_=nplanes_=0; top_left_=0; }
//: Set this view to look at someone else's memory data.
// If the data goes out of scope then this view could be invalid, and
// there's no way of knowing until it's too late -- so take care!
//
// Note that though top_left is passed in as const, the data may be manipulated
// through the view.
void set_to_memory(const T* top_left, unsigned ni, unsigned nj, unsigned nplanes,
vcl_ptrdiff_t i_step, vcl_ptrdiff_t j_step, vcl_ptrdiff_t plane_step);
//: Fill view with given value
void fill(T value);
//: Print a 1-line summary of contents
virtual void print(vcl_ostream&) const;
//: Return class name
virtual vcl_string is_a() const;
//: True if this is (or is derived from) class s
virtual bool is_class(vcl_string const& s) const;
//: Return a description of the concrete data pixel type.
// The value corresponds directly to pixel_type.
inline vil_pixel_format pixel_format() const { return vil_pixel_format_of(T()); }
//: True if they share same view of same image data.
// This does not do a deep equality on image data. If the images point
// to different image data objects that contain identical images, then
// the result will still be false.
bool operator==(const vil_image_view_base& other) const;
//: True if they do not share same view of same image data.
// This does not do a deep inequality on image data. If the images point
// to different image data objects that contain identical images, then
// the result will still be true.
inline bool operator!=(const vil_image_view_base& rhs) const { return !operator==(rhs); }
//: Provides an ordering.
// Useful for ordered containers.
// There is no guaranteed meaning to the less than operator, except that
// (a<b && b<a) is false and !(a<b) && !(b<a) is equivalent to a==b
bool operator<(const vil_image_view_base& rhs) const;
//: Provides an ordering.
inline bool operator>=(const vil_image_view_base& rhs) const { return !operator<(rhs); }
//: Provides an ordering.
bool operator>(const vil_image_view_base& rhs) const;
//: Provides an ordering.
inline bool operator<=(const vil_image_view_base & rhs) const { return !operator>(rhs); }
//: Copy a view. The rhs and lhs will point to the same image data.
const vil_image_view<T>& operator=(const vil_image_view<T>& rhs);
//: Copy a view. The rhs and lhs will point to the same image data.
// You can assign a vil_image_view<compound_type<T>> to a vil_image_view<T>
// in all reasonable cases - the lhs will have as many planes as the rhs has
// components. You can assign a vil_image_view<T> to a vil_image_view<compound_type<T>>
// when the underlying data is formatted appropriately and the lhs has
// as many components as the rhs has planes. O(1).
// If the view types are not compatible this object will be set to empty.
const vil_image_view<T>& operator=(const vil_image_view_base & rhs);
//: Copy a view. The rhs and lhs will point to the same image data.
// You can assign a vil_image_view<compound_type<T>> to a vil_image_view<T>
// in all reasonable cases - the lhs will have as many planes as the rhs has
// components. You can assign a vil_image_view<T> to a vil_image_view<compound_type<T>>
// when the underlying data is formatted appropriately and the lhs has
// as many components as the rhs has planes. O(1).
// If the view types are not compatible this object will be set to empty.
// If the pointer is null, this object will be set to empty.
// See also vil_convert_to_component_order().
inline const vil_image_view<T>& operator=(const vil_image_view_base_sptr& rhs)
{
if (!rhs) clear();
else *this = *rhs;
return *this;
}
};
//: Print a 1-line summary of contents
template <class T>
inline
vcl_ostream& operator<<(vcl_ostream& s, vil_image_view<T> const& im)
{
im.print(s); return s;
}
//: True if the actual images are identical.
// $\bigwedge_{i,j,p} {\textstyle src}(i,j,p) == {\textstyle dest}(i,j,p)$
// The data may be formatted differently in each memory chunk.
// O(size).
// \relates vil_image_view
template<class T>
bool vil_image_view_deep_equality(const vil_image_view<T> &lhs, const vil_image_view<T> &rhs);
#endif // vil_image_view_h_

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks