Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
linearhostmemory.h
1 #pragma once
2 
3 #include <stdio.h>
4 #include <assert.h>
5 #include <cstdlib>
6 #include <string.h> // memcpy
7 #include <thrust/memory.h>
8 #include <type_traits>
9 
10 #include "linearmemory.h"
11 
12 template<typename, int> class ndarray_ref;
13 
14 namespace boost
15 {
16  namespace python
17  {
18  namespace api
19  {
20  class object;
21  }
22  }
23 }
24 
25 class mxArray_tag;
26 
27 namespace iu {
28 
32 template<typename PixelType, unsigned int Ndim>
33 class LinearHostMemory: public LinearMemory<Ndim>
34 {
35 public:
37  typedef PixelType pixel_type;
38 
41  LinearMemory<Ndim>(), data_(0), ext_data_pointer_(false)
42  {
43  }
44 
47  {
48  if ((!ext_data_pointer_) && (data_ != NULL))
49  {
50  free(data_);
51  data_ = 0;
52  }
53  }
54 
59  LinearMemory<Ndim>(size), data_(0), ext_data_pointer_(false)
60  {
61  data_ = (PixelType*) malloc(this->numel() * sizeof(PixelType));
62  if (data_ == 0)
63  throw std::bad_alloc();
64  }
65 
70  LinearHostMemory(const unsigned int& numel) :
71  LinearMemory<Ndim>(numel), data_(0), ext_data_pointer_(false)
72  {
73  data_ = (PixelType*) malloc(this->numel() * sizeof(PixelType));
74  if (data_ == 0)
75  throw std::bad_alloc();
76  }
77 
83  LinearHostMemory(PixelType* host_data, const Size<Ndim>& size,
84  bool ext_data_pointer = false) :
85  LinearMemory<Ndim>(size), data_(0), ext_data_pointer_(ext_data_pointer)
86  {
87  if (host_data == 0)
88  throw IuException("input data not valid", __FILE__, __FUNCTION__,
89  __LINE__);
90  if (ext_data_pointer_)
91  {
92  // This uses the external data pointer as internal data pointer.
93  data_ = host_data;
94  }
95  else
96  {
97  // allocates an internal data pointer and copies the external data onto it.
98  data_ = (PixelType*) malloc(this->numel() * sizeof(PixelType));
99  if (data_ == 0)
100  throw std::bad_alloc();
101  memcpy(data_, host_data, this->numel() * sizeof(PixelType));
102  }
103  }
104 
110  LinearHostMemory(PixelType* host_data, const unsigned int& numel,
111  bool ext_data_pointer = false) :
112  LinearMemory<Ndim>(numel), data_(0), ext_data_pointer_(ext_data_pointer)
113  {
114  if (host_data == 0)
115  throw IuException("input data not valid", __FILE__, __FUNCTION__,
116  __LINE__);
117  if (ext_data_pointer_)
118  {
119  // This uses the external data pointer as internal data pointer.
120  data_ = host_data;
121  }
122  else
123  {
124  // allocates an internal data pointer and copies the external data onto it.
125  data_ = (PixelType*) malloc(this->numel() * sizeof(PixelType));
126  if (data_ == 0)
127  throw std::bad_alloc();
128  memcpy(data_, host_data, this->numel() * sizeof(PixelType));
129  }
130  }
131 
137  PixelType* data(unsigned int offset = 0)
138  {
139  if (offset >= this->numel())
140  {
141  std::stringstream msg;
142  msg << "Index (" << offset << ") out of range (" << this->numel() << ").";
143  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
144  }
145  return &(data_[offset]);
146  }
147 
153  const PixelType* data(unsigned int offset = 0) const
154  {
155  if (offset >= this->numel())
156  {
157  std::stringstream msg;
158  msg << "Offset (" << offset << ") out of range (" << this->numel() << ").";
159  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
160  }
161  return reinterpret_cast<const PixelType*>(&(data_[offset]));
162  }
163 
165  virtual size_t bytes() const
166  {
167  return this->numel() * sizeof(PixelType);
168  }
169 
171  virtual unsigned int bitDepth() const
172  {
173  return 8 * sizeof(PixelType);
174  }
175 
179  thrust::pointer<PixelType, thrust::host_system_tag> begin(void)
180  {
181  return thrust::pointer<PixelType, thrust::host_system_tag>(data());
182  }
183 
187  thrust::pointer<PixelType, thrust::host_system_tag> end(void)
188  {
189  return thrust::pointer<PixelType, thrust::host_system_tag>(
190  data() + this->numel());
191  }
192 
194  virtual bool onDevice() const
195  {
196  return false;
197  }
198 
203  PixelType& getPixel(const unsigned int& idx)
204  {
205  if (idx >= this->numel())
206  {
207  std::stringstream msg;
208  msg << "Index (" << idx << ") out of range (" << this->numel() << ").";
209  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
210  }
211 
212  return this->data_[idx];
213  }
214 
220  template<typename ResultType = PixelType>
221  typename std::enable_if<(Ndim > 1), ResultType&>::type getPixel(
222  const unsigned int& idx0, const unsigned int& idx1)
223  {
224  return data_[getLinearIndex(idx0, idx1)];
225  }
226 
233  template<typename ResultType = PixelType>
234  typename std::enable_if<(Ndim > 2), ResultType&>::type getPixel(
235  const unsigned int& idx0, const unsigned int& idx1,
236  const unsigned int& idx2)
237  {
238  return data_[getLinearIndex(idx0, idx1, idx2)];
239  }
240 
248  template<typename ResultType = PixelType>
249  typename std::enable_if<(Ndim > 3), ResultType&>::type getPixel(
250  const unsigned int& idx0, const unsigned int& idx1,
251  const unsigned int& idx2, const unsigned int& idx3)
252  {
253  return data_[getLinearIndex(idx0, idx1, idx2, idx3)];
254  }
255 
264  template<typename ResultType = PixelType>
265  typename std::enable_if<(Ndim > 4), ResultType&>::type getPixel(
266  const unsigned int& idx0, const unsigned int& idx1,
267  const unsigned int& idx2, const unsigned int& idx3,
268  const unsigned int& idx4)
269  {
270  return data_[getLinearIndex(idx0, idx1, idx2, idx3, idx4)];
271  }
272 
275 
278 
289  LinearHostMemory(boost::python::api::object& py_arr);
290 
294  LinearHostMemory(const mxArray_tag& mex_arr);
295 
296 private:
298  PixelType* data_;
300  bool ext_data_pointer_;
301 
302 private:
306  LinearHostMemory& operator=(const LinearHostMemory&);
307 
313  template<typename ResultType = unsigned int>
314  typename std::enable_if<(Ndim > 1), ResultType>::type getLinearIndex(
315  const unsigned int& idx0, const unsigned int& idx1)
316  {
317  if (idx0 >= this->size()[0] || idx1 >= this->size()[1])
318  {
319  std::stringstream msg;
320  msg << "Index (" << idx0 << ", " << idx1 << ") out of range ("
321  << this->size() << ").";
322  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
323  }
324 
325  unsigned int linear_idx = idx0;
326  linear_idx += this->stride()[1] * idx1;
327  return linear_idx;
328  }
329 
336  template<typename ResultType = unsigned int>
337  typename std::enable_if<(Ndim > 2), ResultType>::type getLinearIndex(
338  const unsigned int& idx0, const unsigned int& idx1,
339  const unsigned int& idx2)
340  {
341  if (idx0 >= this->size()[0] || idx1 >= this->size()[1]
342  || idx2 >= this->size()[2])
343  {
344  std::stringstream msg;
345  msg << "Index (" << idx0 << ", " << idx1 << ", " << idx2
346  << ") out of range (" << this->size() << ").";
347  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
348  }
349 
350  unsigned int linear_idx = idx0;
351  linear_idx += this->stride()[1] * idx1;
352  linear_idx += this->stride()[2] * idx2;
353  return linear_idx;
354  }
355 
363  template<typename ResultType = unsigned int>
364  typename std::enable_if<(Ndim > 3), ResultType>::type getLinearIndex(
365  const unsigned int& idx0, const unsigned int& idx1,
366  const unsigned int& idx2, const unsigned int& idx3)
367  {
368  if (idx0 >= this->size()[0] || idx1 >= this->size()[1]
369  || idx2 >= this->size()[2] || idx3 >= this->size()[3])
370  {
371  std::stringstream msg;
372  msg << "Index (" << idx0 << ", " << idx1 << ", " << idx2 << ", " << idx3
373  << ") out of range (" << this->size() << ").";
374  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
375  }
376 
377  unsigned int linear_idx = idx0;
378  linear_idx += this->stride()[1] * idx1;
379  linear_idx += this->stride()[2] * idx2;
380  linear_idx += this->stride()[3] * idx3;
381  return linear_idx;
382  }
383 
392  template<typename ResultType = unsigned int>
393  typename std::enable_if<(Ndim > 4), ResultType>::type getLinearIndex(
394  const unsigned int& idx0, const unsigned int& idx1,
395  const unsigned int& idx2, const unsigned int& idx3,
396  const unsigned int& idx4)
397  {
398  if (idx0 >= this->size()[0] || idx1 >= this->size()[1]
399  || idx2 >= this->size()[2] || idx3 >= this->size()[3]
400  || idx4 >= this->size()[4])
401 
402  {
403  std::stringstream msg;
404  msg << "Index (" << idx0 << ", " << idx1 << ", " << idx2 << ", " << idx3
405  << ", " << idx4 << ") out of range (" << this->size() << ").";
406  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
407  }
408 
409  unsigned int linear_idx = idx0;
410  linear_idx += this->stride()[1] * idx1;
411  linear_idx += this->stride()[2] * idx2;
412  linear_idx += this->stride()[3] * idx3;
413  linear_idx += this->stride()[4] * idx4;
414  return linear_idx;
415  }
416 };
417 
420 
421 } // namespace iu
422 
423 
std::enable_if<(Ndim > 3), ResultType & >::type getPixel(const unsigned int &idx0, const unsigned int &idx1, const unsigned int &idx2, const unsigned int &idx3)
Definition: linearhostmemory.h:249
thrust::pointer< PixelType, thrust::host_system_tag > begin(void)
Definition: linearhostmemory.h:179
virtual ~LinearHostMemory()
Definition: linearhostmemory.h:46
std::enable_if<(Ndim > 2), ResultType & >::type getPixel(const unsigned int &idx0, const unsigned int &idx1, const unsigned int &idx2)
Definition: linearhostmemory.h:234
unsigned int numel() const
Definition: linearmemory.h:105
virtual unsigned int bitDepth() const
Definition: linearhostmemory.h:171
Linear host memory class.
Definition: linearhostmemory.h:33
LinearHostMemory(const Size< Ndim > &size)
Definition: linearhostmemory.h:58
Exceptions with additional error information.
Definition: coredefs.h:32
std::enable_if<(Ndim > 4), ResultType & >::type getPixel(const unsigned int &idx0, const unsigned int &idx1, const unsigned int &idx2, const unsigned int &idx3, const unsigned int &idx4)
Definition: linearhostmemory.h:265
virtual size_t bytes() const
Definition: linearhostmemory.h:165
LinearHostMemory(const unsigned int &numel)
Definition: linearhostmemory.h:70
Definition: image_cpu.h:7
LinearHostMemory()
Definition: linearhostmemory.h:40
Base class for linear memory classes.
Definition: linearmemory.h:61
LinearHostMemory(PixelType *host_data, const Size< Ndim > &size, bool ext_data_pointer=false)
Definition: linearhostmemory.h:83
Size< Ndim > size() const
Definition: linearmemory.h:121
const PixelType * data(unsigned int offset=0) const
Definition: linearhostmemory.h:153
PixelType & getPixel(const unsigned int &idx)
Definition: linearhostmemory.h:203
thrust::pointer< PixelType, thrust::host_system_tag > end(void)
Definition: linearhostmemory.h:187
PixelType pixel_type
Definition: linearhostmemory.h:37
Size< Ndim > stride() const
Definition: linearmemory.h:127
std::enable_if<(Ndim > 1), ResultType & >::type getPixel(const unsigned int &idx0, const unsigned int &idx1)
Definition: linearhostmemory.h:221
Main class for N-dimensional unsigned int vectors (size vectors).
Definition: vector.h:460
PixelType * data(unsigned int offset=0)
Definition: linearhostmemory.h:137
LinearHostMemory(PixelType *host_data, const unsigned int &numel, bool ext_data_pointer=false)
Definition: linearhostmemory.h:110
ndarray_ref< PixelType, Ndim > ref() const
virtual bool onDevice() const
Definition: linearhostmemory.h:194