00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #ifndef QVCUDAIMAGE_H
00026 #define QVCUDAIMAGE_H
00027
00028 #include <cuda.h>
00029 #include <cuda_runtime.h>
00030
00031 #include <cutil.h>
00032
00033
00034
00035
00036 #include <QVImage>
00037
00047 template <typename Type, int Channels = 1> class QVCUDAImage
00048 {
00049 public:
00053 QVCUDAImage() : cols(0), rows(0), step(0), device_data(NULL) { };
00054
00059 QVCUDAImage(QVImage<Type,Channels> qvimage) : device_data(NULL)
00060 {
00061 uploadToDevice(qvimage);
00062 };
00063
00069 QVCUDAImage(const int c, const int r) : device_data(NULL) { resize(c,r); };
00070
00072 QVCUDAImage(const QVCUDAImage<Type,Channels> &src)
00073 {
00074 resize(src.getCols(),src.getRows());
00075 CUDA_SAFE_CALL(cudaMemcpy2D(device_data, step, src.getData(), src.getStep(), sizeof(Type)*src.getCols()*Channels, src.getRows(), cudaMemcpyDeviceToDevice));
00076 };
00077
00079 QVCUDAImage& operator=(const QVCUDAImage<Type,Channels> &src)
00080 {
00081 resize(src.getCols(),src.getRows());
00082 CUDA_SAFE_CALL(cudaMemcpy2D(device_data, step, src.getData(), src.getStep(), sizeof(Type)*src.getCols()*Channels, src.getRows(), cudaMemcpyDeviceToDevice));
00083 return *this;
00084 };
00085
00087 ~QVCUDAImage() { if(device_data != NULL) CUDA_SAFE_CALL(cudaFree(device_data)); };
00088
00090 int getCols() const { return cols; }
00091
00093 int getRows() const { return rows; }
00094
00096 int getStep() const { return step; }
00097
00099 Type *getData() const { return device_data; }
00100
00106 void resize(const int c, const int r)
00107 {
00108 if(device_data != NULL) CUDA_SAFE_CALL(cudaFree(device_data));
00109 cols = c; rows = r;
00110 size_t step_size_t;
00111 CUDA_SAFE_CALL(cudaMallocPitch((void **)&device_data,&step_size_t,sizeof(Type)*cols*Channels,rows));
00112 step = step_size_t;
00113 }
00114
00116 void uploadToDevice(const QVImage<Type,Channels> qvimage)
00117 {
00118 resize(qvimage.getCols(),qvimage.getRows());
00119 CUDA_SAFE_CALL(cudaMemcpy2D(device_data, step, qvimage.getReadData(), qvimage.getStep(), sizeof(Type)*qvimage.getCols()*Channels, qvimage.getRows(), cudaMemcpyHostToDevice));
00120 };
00121
00123 void downloadFromDevice(QVImage<Type,Channels> &qvimage) const
00124 {
00125 qvimage = QVImage<Type,Channels>(cols,rows);
00126 CUDA_SAFE_CALL(cudaMemcpy2D(qvimage.getWriteData(), qvimage.getStep(), device_data, step, sizeof(Type)*cols*Channels, rows, cudaMemcpyDeviceToHost));
00127 };
00128
00129 private:
00130 int cols, rows, step;
00131 Type *device_data;
00132
00133 };
00134
00135 #endif