7 #include <thrust/memory.h>
10 #include "linearmemory.h"
32 template<
typename PixelType,
unsigned int Ndim>
48 if ((!ext_data_pointer_) && (data_ != NULL))
59 LinearMemory<Ndim>(size), data_(0), ext_data_pointer_(false)
61 data_ = (PixelType*) malloc(this->
numel() *
sizeof(PixelType));
63 throw std::bad_alloc();
71 LinearMemory<Ndim>(numel), data_(0), ext_data_pointer_(false)
73 data_ = (PixelType*) malloc(this->
numel() *
sizeof(PixelType));
75 throw std::bad_alloc();
84 bool ext_data_pointer =
false) :
85 LinearMemory<Ndim>(size), data_(0), ext_data_pointer_(ext_data_pointer)
88 throw IuException(
"input data not valid", __FILE__, __FUNCTION__,
90 if (ext_data_pointer_)
98 data_ = (PixelType*) malloc(this->
numel() *
sizeof(PixelType));
100 throw std::bad_alloc();
101 memcpy(data_, host_data, this->
numel() *
sizeof(PixelType));
111 bool ext_data_pointer =
false) :
112 LinearMemory<Ndim>(numel), data_(0), ext_data_pointer_(ext_data_pointer)
115 throw IuException(
"input data not valid", __FILE__, __FUNCTION__,
117 if (ext_data_pointer_)
125 data_ = (PixelType*) malloc(this->
numel() *
sizeof(PixelType));
127 throw std::bad_alloc();
128 memcpy(data_, host_data, this->
numel() *
sizeof(PixelType));
137 PixelType*
data(
unsigned int offset = 0)
139 if (offset >= this->
numel())
141 std::stringstream msg;
142 msg <<
"Index (" << offset <<
") out of range (" << this->
numel() <<
").";
143 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
145 return &(data_[offset]);
153 const PixelType*
data(
unsigned int offset = 0)
const
155 if (offset >= this->
numel())
157 std::stringstream msg;
158 msg <<
"Offset (" << offset <<
") out of range (" << this->
numel() <<
").";
159 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
161 return reinterpret_cast<const PixelType*
>(&(data_[offset]));
167 return this->
numel() *
sizeof(PixelType);
173 return 8 *
sizeof(PixelType);
179 thrust::pointer<PixelType, thrust::host_system_tag>
begin(
void)
181 return thrust::pointer<PixelType, thrust::host_system_tag>(
data());
187 thrust::pointer<PixelType, thrust::host_system_tag>
end(
void)
189 return thrust::pointer<PixelType, thrust::host_system_tag>(
205 if (idx >= this->
numel())
207 std::stringstream msg;
208 msg <<
"Index (" << idx <<
") out of range (" << this->
numel() <<
").";
209 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
212 return this->data_[idx];
220 template<
typename ResultType = PixelType>
221 typename std::enable_if<(Ndim > 1), ResultType&>::type
getPixel(
222 const unsigned int& idx0,
const unsigned int& idx1)
224 return data_[getLinearIndex(idx0, idx1)];
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)
238 return data_[getLinearIndex(idx0, idx1, idx2)];
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)
253 return data_[getLinearIndex(idx0, idx1, idx2, idx3)];
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)
270 return data_[getLinearIndex(idx0, idx1, idx2, idx3, idx4)];
300 bool ext_data_pointer_;
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)
317 if (idx0 >= this->
size()[0] || idx1 >= this->
size()[1])
319 std::stringstream msg;
320 msg <<
"Index (" << idx0 <<
", " << idx1 <<
") out of range ("
321 << this->
size() <<
").";
322 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
325 unsigned int linear_idx = idx0;
326 linear_idx += this->
stride()[1] * idx1;
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)
341 if (idx0 >= this->
size()[0] || idx1 >= this->
size()[1]
342 || idx2 >= this->
size()[2])
344 std::stringstream msg;
345 msg <<
"Index (" << idx0 <<
", " << idx1 <<
", " << idx2
346 <<
") out of range (" << this->
size() <<
").";
347 throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
350 unsigned int linear_idx = idx0;
351 linear_idx += this->
stride()[1] * idx1;
352 linear_idx += this->
stride()[2] * idx2;
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)
368 if (idx0 >= this->
size()[0] || idx1 >= this->
size()[1]
369 || idx2 >= this->
size()[2] || idx3 >= this->
size()[3])
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__);
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;
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)
398 if (idx0 >= this->
size()[0] || idx1 >= this->
size()[1]
399 || idx2 >= this->
size()[2] || idx3 >= this->
size()[3]
400 || idx4 >= this->
size()[4])
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__);
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;
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