PARP Research Group University of Murcia, Spain


src/qvcuda/qvcudaimage.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008, 2009. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #ifndef QVCUDAIMAGE_H
00026 #define QVCUDAIMAGE_H
00027 
00028 #include <cuda.h>
00029 #include <cuda_runtime.h>
00030 //#include "cutil.h"
00031 #include <cutil.h>
00032 
00033 //#include <qvcuda/qvcudautils.h>
00034 //#include <qvcuda/qvcudaip.h>
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         // Type *texture_data;
00133         };
00134 
00135 #endif



QVision framework. PARP research group, copyright 2007, 2008.