cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
svlSampleCUDAImageCustom.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Balazs Vagvolgyi
7  Created on: 2011
8 
9  (C) Copyright 2006-2011 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 #ifndef _svlSampleCUDAImageCustom_h
23 #define _svlSampleCUDAImageCustom_h
24 
28 
29 // Always include last!
31 
32 
33 extern "C" void* cisst_cudaMalloc(unsigned int size);
34 extern "C" bool cisst_cudaMemset(void* devPtr, unsigned char value, unsigned int size);
35 extern "C" bool cisst_cudaMemcpyHostToDevice(void* devDestPtr, const void* hostSrcPtr, unsigned int size);
36 extern "C" bool cisst_cudaMemcpyDeviceToDevice(void* devDestPtr, const void* devSrcPtr, unsigned int size);
37 extern "C" bool cisst_cudaMemcpyDeviceToHost(void* hostDestPtr, const void* devSrcPtr, unsigned int size);
38 extern "C" bool cisst_cudaFree(void* devPtr);
39 
40 
41 template <class _ValueType, unsigned int _DataChannels, unsigned int _VideoChannels>
43 {
45 
47 
48 public:
49 
51  // Constructors //
53 
56  {
57  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
58  ImageBuffer[vch] = 0;
59  Width[vch] = 0;
60  Height[vch] = 0;
61  }
62  }
63 
65  svlSampleCUDAImage(other)
66  {
67  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
68  ImageBuffer[vch] = 0;
69  Width[vch] = 0;
70  Height[vch] = 0;
71  }
72  CopyOf(other);
73  }
74 
75 
77  // Destructor //
79 
81  {
82  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
83  cisst_cudaFree(ImageBuffer[vch]);
84  }
85  }
86 
88  // Operators //
90 
92  {
93  CopyOf(other);
94  return *this;
95  }
96 
97 
99  // Inherited from svlSample //
101 
103  {
104  return new ThisType;
105  }
106 
108  {
109  if (IsTypeUInt8<_ValueType>(static_cast<_ValueType>(0))) {
110  if (_DataChannels == 1) {
111  if (_VideoChannels == 1) return svlTypeCUDAImageMono8;
112  if (_VideoChannels == 2) return svlTypeCUDAImageMono8Stereo;
113  }
114  if (_DataChannels == 3) {
115  if (_VideoChannels == 1) return svlTypeCUDAImageRGB;
116  if (_VideoChannels == 2) return svlTypeCUDAImageRGBStereo;
117  }
118  if (_DataChannels == 4) {
119  if (_VideoChannels == 1) return svlTypeCUDAImageRGBA;
120  if (_VideoChannels == 2) return svlTypeCUDAImageRGBAStereo;
121  }
122  }
123  if (IsTypeUInt16<_ValueType>(static_cast<_ValueType>(0))) {
124  if (_DataChannels == 1) {
125  if (_VideoChannels == 1) return svlTypeCUDAImageMono16;
126  if (_VideoChannels == 2) return svlTypeCUDAImageMono16Stereo;
127  }
128  }
129  if (IsTypeUInt32<_ValueType>(static_cast<_ValueType>(0))) {
130  if (_DataChannels == 1) {
131  if (_VideoChannels == 1) return svlTypeCUDAImageMono32;
132  if (_VideoChannels == 2) return svlTypeCUDAImageMono32Stereo;
133  }
134  }
135  if (IsTypeFloat<_ValueType>(static_cast<_ValueType>(0))) {
136  if (_DataChannels == 3 && _VideoChannels == 1) return svlTypeCUDAImage3DMap;
137  }
138  return svlTypeInvalid;
139  }
140 
141  int SetSize(const svlSample* sample)
142  {
143  const svlSampleImage* sampleimage = dynamic_cast<const svlSampleImage*>(sample);
144  if (sampleimage) {
145  unsigned int samplevideochannels = sampleimage->GetVideoChannels();
146  for (unsigned int vch = 0; vch < _VideoChannels && vch < samplevideochannels; vch ++) {
147  SetSize(vch, sampleimage->GetWidth(vch), sampleimage->GetHeight(vch));
148  }
149  return SVL_OK;
150  }
151  else {
152  const svlSampleMatrix* samplematrix = dynamic_cast<const svlSampleMatrix*>(sample);
153  if (samplematrix) {
154  SetSize(samplematrix->GetCols(), samplematrix->GetRows());
155  return SVL_OK;
156  }
157  }
158  return SVL_FAIL;
159  }
160 
161  int SetSize(const svlSample& sample)
162  {
163  const svlSampleImage* sampleimage = dynamic_cast<const svlSampleImage*>(&sample);
164  if (sampleimage) {
165  unsigned int samplevideochannels = sampleimage->GetVideoChannels();
166  for (unsigned int vch = 0; vch < _VideoChannels && vch < samplevideochannels; vch ++) {
167  SetSize(vch, sampleimage->GetWidth(vch), sampleimage->GetHeight(vch));
168  }
169  return SVL_OK;
170  }
171  else {
172  const svlSampleMatrix* samplematrix = dynamic_cast<const svlSampleMatrix*>(&sample);
173  if (samplematrix) {
174  SetSize(samplematrix->GetCols(), samplematrix->GetRows());
175  return SVL_OK;
176  }
177  }
178  return SVL_FAIL;
179  }
180 
181  int CopyOf(const svlSample* sample)
182  {
183  if (!sample) return SVL_FAIL;
184  const ThisType* cuda_image = dynamic_cast<const ThisType*>(sample);
185  if (cuda_image) {
186  // It is the same type of sample
187  // Allocate memory on the device
188  if (SetSize(cuda_image) != SVL_OK) return SVL_FAIL;
189  // Perform DEVICE to DEVICE memory copy
190  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
192  }
193  }
194  else {
195  // Try if it is a regular image sample of the same type
196  const svlSampleImage* regular_image = dynamic_cast<const svlSampleImage*>(sample);
197  if (!regular_image ||
198  regular_image->GetVideoChannels() != GetVideoChannels() ||
199  regular_image->GetPixelType() != GetPixelType()) return SVL_FAIL;
200  // OK: same type sample
201  // Allocate memory on the device
202  if (SetSize(regular_image) != SVL_OK) return SVL_FAIL;
203  // Perform HOST to DEVICE memory copy
204  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
206  }
207  }
208  SetTimestamp(sample->GetTimestamp());
209 
210  return SVL_OK;
211  }
212 
213  int CopyOf(const svlSample& sample)
214  {
215  return CopyOf(&sample);
216  }
217 
218  bool IsInitialized() const
219  {
220  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
221  if (Width[vch] < 1 || Height[vch] < 1) return false;
222  }
223  return true;
224  }
225 
226  unsigned int GetDataSize() const
227  {
228  return GetDataSize(0);
229  }
230 
232  // Inherited from svlSampleImage //
234 
235  void SetSize(const unsigned int width, const unsigned int height)
236  {
237  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
238  SetSize(vch, width, height);
239  }
240  }
241 
242  void SetSize(const unsigned int videochannel, const unsigned int width, const unsigned int height)
243  {
244  if (videochannel < _VideoChannels) {
245  if (Width[videochannel] == width && Height[videochannel] == height) return;
246 
247  if (ImageBuffer[videochannel]) {
248  cisst_cudaFree(ImageBuffer[videochannel]);
249  ImageBuffer[videochannel] = 0;
250  Width[videochannel] = 0;
251  Height[videochannel] = 0;
252  }
253 
254  if (width > 0 && height > 0) {
255  ImageBuffer[videochannel] = cisst_cudaMalloc(width * GetBPP() * height);
256  if (ImageBuffer[videochannel]) {
257  Width[videochannel] = width;
258  Height[videochannel] = height;
259  }
260  }
261  }
262  }
263 
264 #if CISST_SVL_HAS_OPENCV
265  int SetSize(const IplImage* ipl_image, const unsigned int videochannel = 0)
266 #else // CISST_SVL_HAS_OPENCV
267  int SetSize(const IplImage* CMN_UNUSED(ipl_image), const unsigned int CMN_UNUSED(videochannel) = 0)
268 #endif // CISST_SVL_HAS_OPENCV
269  {
270 #if CISST_SVL_HAS_OPENCV
271  if (ipl_image && videochannel >= _VideoChannels) {
272  SetSize(videochannel, ipl_image->width, ipl_image->height);
273  return SVL_OK;
274  }
275 #endif // CISST_SVL_HAS_OPENCV
276  return SVL_FAIL;
277  }
278 
279  unsigned int GetVideoChannels() const
280  {
281  return _VideoChannels;
282  }
283 
284  unsigned int GetDataChannels() const
285  {
286  return _DataChannels;
287  }
288 
289  unsigned int GetBPP() const
290  {
291  return (sizeof(_ValueType) * _DataChannels);
292  }
293 
294  unsigned int GetWidth(const unsigned int videochannel = 0) const
295  {
296  if (videochannel < _VideoChannels) return Width[videochannel];
297  return 0;
298  }
299 
300  unsigned int GetHeight(const unsigned int videochannel = 0) const
301  {
302  if (videochannel < _VideoChannels) return Height[videochannel];
303  return 0;
304  }
305 
306  unsigned int GetRowStride(const unsigned int videochannel = 0) const
307  {
308  if (videochannel < _VideoChannels) return Width[videochannel] * GetBPP();
309  return 0;
310  }
311 
312  unsigned int GetDataSize(const unsigned int videochannel) const
313  {
314  if (videochannel < _VideoChannels) {
315  return (GetBPP() * GetWidth(videochannel) * GetHeight(videochannel));
316  }
317  return 0;
318  }
319 
320 
322  // Inherited from svlSampleCUDAImage //
324 
325  int CopyTo(svlSample* sample) const
326  {
327  if (!sample) return SVL_FAIL;
328  ThisType* cuda_image = dynamic_cast<ThisType*>(sample);
329  if (cuda_image) {
330  // It is the same type of sample
331  // Allocate memory on the device
332  if (cuda_image->SetSize(this) != SVL_OK) return SVL_FAIL;
333  // Perform DEVICE to DEVICE memory copy
334  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
336  }
337  }
338  else {
339  // Try if it is a regular image sample of the same type
340  svlSampleImage* regular_image = dynamic_cast<svlSampleImage*>(sample);
341  if (!regular_image ||
342  regular_image->GetVideoChannels() != GetVideoChannels() ||
343  regular_image->GetPixelType() != GetPixelType()) return SVL_FAIL;
344  // OK: same type sample
345  // Allocate memory on the device
346  if (regular_image->SetSize(this) != SVL_OK) return SVL_FAIL;
347  // Perform DEVICE to HOST memory copy
348  for (unsigned int vch = 0; vch < _VideoChannels; vch ++) {
350  }
351  }
352  sample->SetTimestamp(GetTimestamp());
353 
354  return SVL_OK;
355  }
356 
357  int CopyTo(svlSample& sample) const
358  {
359  return CopyTo(&sample);
360  }
361 
362  void* GetCUDAPointer(const unsigned int videochannel = 0)
363  {
364  if (videochannel < _VideoChannels) return ImageBuffer[videochannel];
365  return 0;
366  }
367 
368  const void* GetCUDAPointer(const unsigned int videochannel = 0) const
369  {
370  if (videochannel < _VideoChannels) return ImageBuffer[videochannel];
371  return 0;
372  }
373 
374 
375 private:
376  void* ImageBuffer[_VideoChannels];
377  unsigned int Width[_VideoChannels];
378  unsigned int Height[_VideoChannels];
379 };
380 
381 #endif // _svlSampleCUDAImageCustom_h
382 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
virtual unsigned int GetBPP() const =0
virtual void * GetCUDAPointer(const unsigned int videochannel=0)=0
int CopyOf(const svlSample &sample)
Definition: svlSampleCUDAImageCustom.h:213
Definition: svlSampleMatrix.h:34
int CopyTo(svlSample *sample) const
Definition: svlSampleCUDAImageCustom.h:325
Definition: svlDefinitions.h:210
Definition: svlDefinitions.h:215
virtual unsigned int GetHeight(const unsigned int videochannel=0) const =0
svlStreamType
Definition: svlDefinitions.h:193
Definition: svlDefinitions.h:214
svlSampleCUDAImageCustom(const ThisType &other)
Definition: svlSampleCUDAImageCustom.h:64
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
virtual unsigned int GetDataSize() const =0
bool cisst_cudaMemcpyDeviceToHost(void *hostDestPtr, const void *devSrcPtr, unsigned int size)
Definition: svlSampleCUDAImageCustom.h:42
virtual svlPixelType GetPixelType() const
svlSample * GetNewInstance() const
Definition: svlSampleCUDAImageCustom.h:102
Definition: svlDefinitions.h:219
unsigned int GetDataChannels() const
Definition: svlSampleCUDAImageCustom.h:284
const void * GetCUDAPointer(const unsigned int videochannel=0) const
Definition: svlSampleCUDAImageCustom.h:368
virtual unsigned int GetWidth(const unsigned int videochannel=0) const =0
int SetSize(const svlSample *sample)
Definition: svlSampleCUDAImageCustom.h:141
Definition: svlDefinitions.h:213
bool cisst_cudaMemcpyHostToDevice(void *devDestPtr, const void *hostSrcPtr, unsigned int size)
unsigned int GetHeight(const unsigned int videochannel=0) const
Definition: svlSampleCUDAImageCustom.h:300
virtual unsigned char * GetUCharPointer()=0
unsigned int GetWidth(const unsigned int videochannel=0) const
Definition: svlSampleCUDAImageCustom.h:294
svlSample & operator=(const svlSample &other)
virtual unsigned int GetHeight(const unsigned int videochannel=0) const =0
svlSampleCUDAImageCustom()
Definition: svlSampleCUDAImageCustom.h:54
void IplImage
Definition: svlTypes.h:70
Definition: svlDefinitions.h:195
virtual unsigned int GetVideoChannels() const =0
bool cisst_cudaMemcpyDeviceToDevice(void *devDestPtr, const void *devSrcPtr, unsigned int size)
int SetSize(const IplImage *CMN_UNUSED(ipl_image), const unsigned int CMN_UNUSED(videochannel)=0)
Definition: svlSampleCUDAImageCustom.h:267
void SetSize(const unsigned int width, const unsigned int height)
Definition: svlSampleCUDAImageCustom.h:235
void * GetCUDAPointer(const unsigned int videochannel=0)
Definition: svlSampleCUDAImageCustom.h:362
unsigned int GetRowStride(const unsigned int videochannel=0) const
Definition: svlSampleCUDAImageCustom.h:306
virtual unsigned int GetRows() const =0
virtual unsigned int GetVideoChannels() const =0
Definition: svlDefinitions.h:216
virtual int CopyTo(svlSample *sample) const =0
void SetSize(const unsigned int videochannel, const unsigned int width, const unsigned int height)
Definition: svlSampleCUDAImageCustom.h:242
svlStreamType GetType() const
Definition: svlSampleCUDAImageCustom.h:107
Definition: svlSampleImage.h:35
int CopyOf(const svlSample *sample)
Definition: svlSampleCUDAImageCustom.h:181
Macros to export the symbols of cisstStereoVision (in a Dll).
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
Definition: svlDefinitions.h:218
virtual int SetSize(const svlSample *sample)=0
int CopyTo(svlSample &sample) const
Definition: svlSampleCUDAImageCustom.h:357
void * cisst_cudaMalloc(unsigned int size)
unsigned int GetBPP() const
Definition: svlSampleCUDAImageCustom.h:289
Definition: svlDefinitions.h:217
bool cisst_cudaMemset(void *devPtr, unsigned char value, unsigned int size)
#define SVL_FAIL
Definition: svlDefinitions.h:124
unsigned int GetDataSize() const
Definition: svlSampleCUDAImageCustom.h:226
bool cisst_cudaFree(void *devPtr)
unsigned int GetDataSize(const unsigned int videochannel) const
Definition: svlSampleCUDAImageCustom.h:312
unsigned int GetVideoChannels() const
Definition: svlSampleCUDAImageCustom.h:279
void SetTimestamp(double ts)
const int CMN_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:331
~svlSampleCUDAImageCustom()
Definition: svlSampleCUDAImageCustom.h:80
bool IsInitialized() const
Definition: svlSampleCUDAImageCustom.h:218
virtual int CopyOf(const svlSample *sample)=0
virtual int SetSize(const svlSample *sample)=0
Definition: svlDefinitions.h:211
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
Definition: svlDefinitions.h:209
#define SVL_OK
Definition: svlDefinitions.h:123
int SetSize(const svlSample &sample)
Definition: svlSampleCUDAImageCustom.h:161
Definition: svlSampleCUDAImage.h:31
virtual unsigned int GetWidth(const unsigned int videochannel=0) const =0
Definition: svlSample.h:31
Definition: svlDefinitions.h:212
double GetTimestamp() const
virtual unsigned int GetCols() const =0