src/qvcore/qvimagebuffer.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. 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 QVIMAGE_BUFFER_H
00026 #define QVIMAGE_BUFFER_H
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <math.h>
00031 
00032 #include <QObject>
00033 #include <QRect>
00034 #include <QDebug>
00035 #include <ipp.h>
00036 
00037 #include <QPoint>
00038 #include <QSharedData>
00039 #include <qvdefines.h>
00040 
00041 #ifndef DOXYGEN_IGNORE_THIS
00042 
00043 template <typename Type = uChar, int Channels = 1> class QVImageBuffer: public QSharedData
00044         {
00045         public:
00046                 //QVImageBuffer();
00047 
00048                 QVImageBuffer(): QSharedData() {};
00049                 QVImageBuffer(const QVImageBuffer<Type, Channels> &);
00050                 QVImageBuffer(uInt cols, uInt rows, uInt step = 0, const Type * buffer = NULL);
00051 
00052                 ~QVImageBuffer()        { delete this->data; }
00053 
00054                 uInt getRows()          const   { return rows; }
00055                 uInt getCols()          const   { return cols; }
00056                 uInt getStep()          const   { return step; }
00057                 uInt getChannels()      const   { return planes; }
00058                 uInt getTypeSize()      const   { return typeSize; }
00059 
00060                 int getDataSize()                       const   { return dataSize; }
00061                 const Type *    const getReadData()     const   { return data; }
00062                 Type * const    getWriteData()          const   { return data; }
00063 
00064         protected:
00065                 Type * data;
00066                 uInt cols, rows, step, planes, dataSize, typeSize;
00067                 void allocData(uInt cols, uInt rows, uInt step = 0);
00068         };
00069 
00070 template <typename Type, int Channels>
00071 void QVImageBuffer<Type,Channels>::allocData(uInt cols, uInt rows, uInt step)
00072         {
00073         qDebug() << "QVImageBuffer<Type,Channels>::allocData(" << cols << "," << rows << "," << step << ")";
00074         Q_ASSERT_X(cols > 0, "QVImageBuffer::allocData()", "cols <= 0");
00075         Q_ASSERT_X(rows > 0, "QVImageBuffer::allocData()", "rows <= 0");
00076         Q_ASSERT_X((step == 0) || (step >= cols), "QVImageBuffer::allocData()", "0 < step < cols");
00077         this->rows = rows;
00078         this->cols = cols;
00079         this->planes = Channels;
00080         this->typeSize = sizeof(Type);
00081 
00082         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): planes" << this->planes;
00083         uInt temp = this->planes * this->typeSize * cols;
00084 
00085         if (step == 0)
00086                 this->step = 8*(uInt)ceil((double)(temp)/8);
00087         else
00088                 this->step = step;
00089 
00090         Q_ASSERT_X( (this->step % 8) == 0, "QVImageBuffer::QVImageBuffer()","step % 8 != 0");
00091         Q_ASSERT_X( this->step >= temp, "QVImageBuffer::QVImageBuffer()","step insufficient");
00092 
00093         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): rows" << this->rows;
00094 
00095         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): planes" << this->planes;
00096         this->dataSize = this->rows * this->step * this->planes;
00097 
00098         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): dataSize" << this->dataSize;
00099 
00100         if ( (this->data != NULL) && (this->cols * this->rows != cols * rows) )
00101                 {
00102                 delete this->data;
00103                 this->data = NULL;
00104                 }
00105 
00106         //qDebug() << "QVImageBuffer<Type,Channels>::allocData(): dataSize" << this->dataSize;
00107 
00108         if (this->data == NULL)
00109                 this->data = new Type[this->dataSize];
00110 
00111         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): step" << this->step;
00112         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): step" << this->getStep();
00113         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): dataSize" << this->dataSize;
00114         qDebug() << "QVImageBuffer<Type,Channels>::allocData(): dataSize" << this->getDataSize();
00115         qDebug() << "QVImageBuffer<Type,Channels>::allocData(" << cols << "," << rows << "," << step << ") <- return";
00116         }
00117 
00118 template <typename Type, int Channels>
00119 QVImageBuffer<Type,Channels>::QVImageBuffer(uInt cols, uInt rows, uInt step, const Type *buffer): QSharedData(),
00120         data(NULL)
00121         {
00122         qDebug() << "QVImageBuffer<Type, Channels>::QVImageBuffer(" << cols << "," << rows << "," << step << ")";
00123         Q_ASSERT_X(cols > 0, "QVImageBuffer::allocData()", "cols <= 0");
00124         Q_ASSERT_X(rows > 0, "QVImageBuffer::allocData()", "rows <= 0");
00125         Q_ASSERT_X((step == 0) || (step >= cols), "QVImageBuffer::allocData()", "0 < step < cols");
00126 
00127         allocData(cols, rows, step);
00128 
00129         if (buffer != NULL)
00130                 // *_* to *_* (copy)
00131                 memcpy(this->data,buffer,this->dataSize);
00132         qDebug() << "QVImageBuffer<Type, Channels>::QVImageBuffer() <- return";
00133         }
00134 #endif
00135 #endif