Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
image_gpu.h
1 #pragma once
2 
3 #include "image.h"
4 #include "image_allocator_gpu.h"
5 #include <thrust/device_ptr.h>
6 
7 template<typename type, int dims> class ndarray_ref;
8 
9 namespace iu {
10 
11 
12 template<typename PixelType, bool zero_reminder>
14  static int stride(int pitch_){
15  return pitch_/sizeof(PixelType);
16  }
17 };
18 
19 template<typename PixelType>
20 struct use_PixelType<PixelType,false>{
21  static int __attribute__((deprecated("this function may return wrong result in case pitch_ is not divisible by sizeof(PixelType) (typically the case when using _C3 images)"))) stride(int pitch_){
22  if(pitch_ % sizeof(PixelType) != 0){
23  throw std::runtime_error("bad! your are using wrong stride, because pitch is not divisible (are you using _C3 images? You shouldn't)");
24  };
25  return pitch_/sizeof(PixelType);
26  }
27 };
28 
29 
30 template<typename PixelType, class Allocator>
34 class ImageGpu : public Image
35 {
36 public:
38  typedef PixelType pixel_type;
39 
42  Image(),
43  data_(0), pitch_(0), ext_data_pointer_(false), texture_(0)
44  {
45  }
46 
48  virtual ~ImageGpu()
49  {
51  {
52  // do not delete externally handeled data pointers.
53  Allocator::free(data_);
54  data_ = 0;
55  }
56  pitch_ = 0;
57 
58  if (texture_)
59  IU_CUDA_SAFE_CALL(cudaDestroyTextureObject(texture_));
60  }
61 
66  ImageGpu(unsigned int _width, unsigned int _height) :
67  Image(_width, _height), data_(0), pitch_(0),
68  ext_data_pointer_(false), texture_(0)
69  {
70  data_ = Allocator::alloc(this->size(), &pitch_);
71  }
72 
77  Image(size), data_(0), pitch_(0),
78  ext_data_pointer_(false), texture_(0)
79  {
80  data_ = Allocator::alloc(size, &pitch_);
81  }
82 
90  ImageGpu(PixelType* _data, unsigned int _width, unsigned int _height,
91  size_t _pitch, bool ext_data_pointer = false) :
92  Image(_width, _height), data_(0), pitch_(0), ext_data_pointer_(ext_data_pointer), texture_(0)
93  {
95  {
96  // This uses the external data pointer as internal data pointer.
97  data_ = _data;
98  pitch_ = _pitch;
99  }
100  else
101  {
102  // allocates an internal data pointer and copies the external data onto it.
103  if(_data == 0)
104  return;
105 
106  data_ = Allocator::alloc(this->size(), &pitch_);
107  Allocator::copy(_data, _pitch, data_, pitch_, this->size());
108  }
109  }
110 
112  PixelType getPixel(unsigned int x, unsigned int y)
113  {
114  PixelType value;
115  IU_CUDA_SAFE_CALL(cudaMemcpy2D(&value, sizeof(PixelType), &data_[y*stride()+x], pitch_,
116  sizeof(PixelType), 1, cudaMemcpyDeviceToHost));
117  return value;
118  }
119 
121  virtual size_t bytes() const
122  {
123  return height()*pitch_;
124  }
125 
127  virtual size_t pitch() const
128  {
129  return pitch_;
130  }
131 
136  virtual size_t stride() const
137  {
139  //return pitch_/sizeof(PixelType);
140  }
141 
143  virtual unsigned int bitDepth() const
144  {
145  return 8*sizeof(PixelType);
146  }
147 
149  virtual bool onDevice() const
150  {
151  return true;
152  }
153 
160  PixelType* data(int ox = 0, int oy = 0)
161  {
162  //return &data_[oy * stride() + ox];
163  return (PixelType*)( (char*)data_ + pitch_*oy + ox*sizeof(PixelType) );
164  }
165 
172  const PixelType* data(int ox = 0, int oy = 0) const
173  {
174  return const_cast<ImageGpu*>(this)->data(ox,oy);
175  //return reinterpret_cast<const PixelType*>(
176  // &data_[oy * stride() + ox]);
177  }
178 
182  thrust::device_ptr<PixelType> begin(void)
183  {
184  return thrust::device_ptr<PixelType>(data());
185  }
186 
190  thrust::device_ptr<PixelType> end(void)
191  {
192  //return thrust::device_ptr<PixelType>(data()+stride()*height());
193  return thrust::device_ptr<PixelType>(data(width()-1,height()-1) + 1);
194  }
195 
201  void prepareTexture(cudaTextureReadMode readMode = cudaReadModeElementType,
202  cudaTextureFilterMode filterMode = cudaFilterModeLinear,
203  cudaTextureAddressMode addressMode = cudaAddressModeClamp)
204  {
205  if (texture_) // delete if already exists
206  IU_CUDA_SAFE_CALL(cudaDestroyTextureObject(texture_));
207 
208  cudaResourceDesc resDesc;
209  memset(&resDesc, 0, sizeof(resDesc));
210 
211  cudaTextureDesc texDesc;
212  memset(&texDesc, 0, sizeof(texDesc));
213 
214  resDesc.resType = cudaResourceTypePitch2D;
215  resDesc.res.pitch2D.devPtr = data();
216  resDesc.res.pitch2D.pitchInBytes = pitch();
217  resDesc.res.pitch2D.width = width();
218  resDesc.res.pitch2D.height = height();
219  resDesc.res.pitch2D.desc = cudaCreateChannelDesc<PixelType>();
220 
221  texDesc.readMode = readMode;
222  texDesc.normalizedCoords = (addressMode == cudaAddressModeClamp) ? false : true;
223  texDesc.addressMode[0] = addressMode;
224  texDesc.addressMode[1] = addressMode;
225  texDesc.filterMode = filterMode;
226 
227  IU_CUDA_SAFE_CALL(cudaCreateTextureObject(&texture_, &resDesc, &texDesc, NULL));
228  }
229 
231  cudaTextureObject_t getTexture()
232  {
233  if (!texture_) // create texture object implicitly
234  this->prepareTexture();
235 
236  return texture_;
237  }
238 
246  inline cudaTextureObject_t getTexture() const
247  {
248  if (!texture_)
249  throw IuException("Warning: getTexture() on const image requires explicit call to prepareTexture(),"
250  " returned cudaTextureObject will be invalid\n", __FILE__, __FUNCTION__, __LINE__);
251 
252  return texture_;
253  }
254 
283  struct KernelData
284  {
286  PixelType* data_;
288  int width_;
290  int height_;
292  int stride_;
293 
299  __device__ PixelType& operator()(int x, int y)
300  {
301  return data_[y*stride_ + x];
302  }
303 
306  : data_(const_cast<PixelType*>(im.data())), width_(im.width()), height_(im.height()),
307  stride_(im.stride())
308  { }
309  };
310 
313 
316 
317 protected:
319  PixelType* data_;
321  size_t pitch_;
325  cudaTextureObject_t texture_;
326 
327 private:
329  ImageGpu(const ImageGpu&);
331  ImageGpu& operator=(const ImageGpu&);
332 };
333 
334 } // namespace iu
335 
336 
iu::Size< 2 > size() const
Definition: image.h:64
const PixelType * data(int ox=0, int oy=0) const
Definition: image_gpu.h:172
__host__ KernelData(const ImageGpu< PixelType, Allocator > &im)
Definition: image_gpu.h:305
Base class for 2D images (pitched memory).
Definition: image.h:30
PixelType pixel_type
Definition: image_gpu.h:38
IUCORE_DLLAPI void copy(const LinearHostMemory_8u_C1 *src, LinearHostMemory_8u_C1 *dst)
cudaTextureObject_t getTexture()
Definition: image_gpu.h:231
thrust::device_ptr< PixelType > end(void)
Definition: image_gpu.h:190
PixelType * data_
Definition: image_gpu.h:319
cudaTextureObject_t texture_
Definition: image_gpu.h:325
__device__ PixelType & operator()(int x, int y)
Definition: image_gpu.h:299
virtual unsigned int bitDepth() const
Definition: image_gpu.h:143
PixelType * data_
Definition: image_gpu.h:286
size_t pitch_
Definition: image_gpu.h:321
void prepareTexture(cudaTextureReadMode readMode=cudaReadModeElementType, cudaTextureFilterMode filterMode=cudaFilterModeLinear, cudaTextureAddressMode addressMode=cudaAddressModeClamp)
Definition: image_gpu.h:201
Exceptions with additional error information.
Definition: coredefs.h:32
ImageGpu(unsigned int _width, unsigned int _height)
Definition: image_gpu.h:66
ImageGpu(PixelType *_data, unsigned int _width, unsigned int _height, size_t _pitch, bool ext_data_pointer=false)
Definition: image_gpu.h:90
Struct pointer KernelData that can be used in CUDA kernels.
Definition: image_gpu.h:283
virtual ~ImageGpu()
Definition: image_gpu.h:48
Definition: image_cpu.h:7
ImageGpu(const iu::Size< 2 > &size)
Definition: image_gpu.h:76
PixelType getPixel(unsigned int x, unsigned int y)
Definition: image_gpu.h:112
virtual bool onDevice() const
Definition: image_gpu.h:149
virtual size_t stride() const
Definition: image_gpu.h:136
int stride_
Definition: image_gpu.h:292
ImageGpu()
Definition: image_gpu.h:41
ndarray_ref< PixelType, 2 > ref() const
bool ext_data_pointer_
Definition: image_gpu.h:323
thrust::device_ptr< PixelType > begin(void)
Definition: image_gpu.h:182
Template specialization for 2-d unsigned int vectors (size vectors).
Definition: vector.h:525
unsigned int width() const
Definition: image.h:72
int width_
Definition: image_gpu.h:288
cudaTextureObject_t getTexture() const
Definition: image_gpu.h:246
virtual size_t pitch() const
Definition: image_gpu.h:127
Definition: image_gpu.h:13
Device 2D image class (pitched memory).
Definition: image_gpu.h:34
int height_
Definition: image_gpu.h:290
PixelType * data(int ox=0, int oy=0)
Definition: image_gpu.h:160
virtual size_t bytes() const
Definition: image_gpu.h:121
unsigned int height() const
Definition: image.h:80