PARP Research Group University of Murcia, Spain


src/qvcore/qvimage.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_H
00026 #define QVIMAGE_H
00027 
00028 #include <QMetaType>
00029 #include <qvcore/qvimagebuffer.h>
00030 #include <QVGenericImage>
00031 #include <QImage>
00032 
00042 #define QVIMAGE_INIT_READ(TYPE, IMAGE)                                          \
00043         const TYPE * __qv_data_##IMAGE##__ = IMAGE.getReadData();               \
00044         const uInt __qv_step_##IMAGE##__ = IMAGE.getStep()/sizeof(TYPE);        \
00045         const uChar __qv_planes_##IMAGE##__  = IMAGE.getChannels();             \
00046         const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - IMAGE.getCols();
00047 
00057 #define QVIMAGE_INIT_WRITE(TYPE, IMAGE)                                         \
00058         TYPE * __qv_data_##IMAGE##__ = IMAGE.getWriteData();                    \
00059         const uInt __qv_step_##IMAGE##__ = IMAGE.getStep()/sizeof(TYPE);        \
00060         const uChar __qv_planes_##IMAGE##__  = IMAGE.getChannels();             \
00061         const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - IMAGE.getCols();
00062 
00072 #define QVIMAGE_PTR_INIT_READ(TYPE, IMAGE)                                      \
00073         const TYPE * __qv_data_##IMAGE##__ = IMAGE->getReadData();              \
00074         const uInt __qv_step_##IMAGE##__ = IMAGE->getStep()/sizeof(TYPE);       \
00075         const uChar __qv_planes_##IMAGE##__  = IMAGE->getChannels();            \
00076         const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - IMAGE->getCols();
00077 
00087 #define QVIMAGE_PTR_INIT_WRITE(TYPE, IMAGE)                                                                             \
00088         TYPE * __qv_data_##IMAGE##__ = IMAGE->getWriteData();                                                           \
00089         const uInt __qv_step_##IMAGE##__ = IMAGE->getStep()/sizeof(TYPE);                                               \
00090         const uChar __qv_planes_##IMAGE##__  = IMAGE->getChannels();                                                    \
00091         const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - __qv_planes_##IMAGE##__ * IMAGE->getCols();
00092 
00102 #define QVIMAGE_PIXEL(IMAGE, Col, Row, Channel) \
00103         (__qv_data_##IMAGE##__ [(Row)* __qv_step_##IMAGE##__ + __qv_planes_##IMAGE##__ *(Col)+(Channel)])
00104 
00114 #define QVIMAGE_PIXEL_PTR(IMAGE, Col, Row, Channel)     \
00115         (& (__qv_data_##IMAGE##__ [(Row)* __qv_step_##IMAGE##__ + __qv_planes_##IMAGE##__ *(Col)+(Channel)]))
00116 
00123 #define QVIMAGE_ROW_INCREMENT_PTR(IMAGE)        ( __qv_step_##IMAGE##__ )
00124 
00132 #define QVIMAGE_COL_INCREMENT_PTR(IMAGE)        ( __qv_planes_##IMAGE##__ )
00133 
00140 #define QVIMAGE_NEXT_LINE_INCREMENT_PTR(IMAGE)  ( __qv_next_line_inc_##IMAGE##__ )
00141 
00152 template <typename Type, int Channels = 1> class QVImage: public QVGenericImage 
00153         {
00154         protected:
00155                 uInt step_div_type_size;
00156                 QSharedDataPointer< QVImageBuffer<Type,Channels> > imageBuffer;
00157 
00158         public:
00164                 QVImage():QVGenericImage()
00165                         {
00166                         this->imageBuffer = new QVImageBuffer<Type, Channels>(1, 1);
00167                         setROI(0,0, this->imageBuffer->getCols(), this->imageBuffer->getRows());
00168                         setAnchor(0,0);
00169                         this->step_div_type_size = getStep()/sizeof(Type);
00170                         }
00171 
00208                 QVImage(uInt cols, uInt rows, uInt step = 0, const Type * buffer = NULL):QVGenericImage()
00209                         {
00210                         this->imageBuffer = new QVImageBuffer<Type, Channels>(cols, rows, step, buffer);
00211                         setROI(0,0, cols, rows);
00212                         setAnchor(0,0);
00213                         this->step_div_type_size = getStep()/sizeof(Type);
00214                         }
00215 
00232                 QVImage(QVImage<uChar,1> const &img);
00233 
00235                 QVImage(QVImage<uChar,3> const &img);
00236 
00238                 QVImage(QVImage<uShort,1> const &img);
00239 
00241                 QVImage(QVImage<uShort,3> const &img);
00242 
00244                 QVImage(QVImage<sShort,1> const &img);
00245 
00247                 QVImage(QVImage<sShort,3> const &img);
00248 
00250                 QVImage(QVImage<sInt,1> const &img);
00251 
00253                 QVImage(QVImage<sInt,3> const &img);
00254 
00256                 QVImage(QVImage<sFloat,1> const &img);
00257 
00259                 QVImage(QVImage<sFloat,3> const &img);
00260 
00270                 QVImage(QVImage<uChar,1> const &red, QVImage<uChar,1> const &green, QVImage<uChar,1> const &blue);
00271 
00273                 QVImage(QVImage<uShort,1> const &red, QVImage<uShort,1> const &green, QVImage<uShort,1> const &blue);
00274 
00276                 QVImage(QVImage<sShort,1> const &red, QVImage<sShort,1> const &green, QVImage<sShort,1> const &blue);
00277 
00279                 QVImage(QVImage<sInt,1> const &red, QVImage<sInt,1> const &green, QVImage<sInt,1> const &blue);
00280 
00282                 QVImage(QVImage<sFloat,1> const &red, QVImage<sFloat,1> const &green, QVImage<sFloat,1> const &blue);
00283 
00284                 // Convert operators for QImage
00286                 QVImage(const QImage &qImage);
00287 
00289                 operator QImage() const;
00290 
00291                 // Misc data.
00293                 const char * getTypeQString() const;
00294 
00296                 uInt getRows()          const   { return imageBuffer->getRows(); }
00297 
00299                 uInt getCols()          const   { return imageBuffer->getCols(); }
00300 
00302                 inline uInt getStep()   const   { return imageBuffer->getStep(); }
00303 
00305                 inline uInt getChannels()       const   { return imageBuffer->getChannels(); }
00306 
00308                 uInt getDataSize()      const   { return imageBuffer->getDataSize(); }
00309 
00311                 uInt getTypeSize()      const   { return imageBuffer->getTypeSize(); }
00312 
00323                 const Type * getReadData()      const   { return imageBuffer->getReadData(); }
00324 
00335                 Type * getWriteData()           { return imageBuffer->getWriteData(); }
00336 
00351                 void set(Type c1 = 0, Type c2 = 0, Type c3 = 0);
00352 
00361                 inline Type &operator()(const uInt col, const uInt row, const uInt channel = 0)
00362                         {
00363                         Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00364                         Q_ASSERT_X(col < getCols(),"QVImage::operator()","col further upper bound");
00365                         Q_ASSERT_X(row < getRows(),"QVImage::operator()","row further upper bound");
00366                         const int idx = step_div_type_size*row + Channels*col + channel;
00367                         Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00368                         Q_ASSERT_X(idx < getDataSize(),"QVImage::operator()","accessing above data");
00369 
00370                         return imageBuffer->getWriteData()[idx];
00371                         }
00372 
00373                 inline Type operator()(const uInt col, const uInt row, const uInt channel = 0) const
00374                         {
00375                         Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00376                         Q_ASSERT_X(col < getCols(),"QVImage::operator()","col further upper bound");
00377                         Q_ASSERT_X(row < getRows(),"QVImage::operator()","row further upper bound");
00378                         const int idx = step_div_type_size*row + Channels*col + channel;
00379                         Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00380                         Q_ASSERT_X(idx < getDataSize(),"QVImage::operator()","accessing above data");
00381 
00382                         return imageBuffer->getReadData()[idx];
00383                         }
00384 
00391                 inline Type &operator()(const QPoint point, const uInt channel = 0)
00392                         {
00393                         Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00394                         Q_ASSERT_X(point.x() < getCols(),"QVImage::operator()","col further upper bound");
00395                         Q_ASSERT_X(point.y() < getRows(),"QVImage::operator()","row further upper bound");
00396                         const int idx = step_div_type_size*point.y() + Channels*point.x() + channel;
00397                         Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00398                         Q_ASSERT_X(idx < getDataSize(),"QVImage::operator()","accessing above data");
00399 
00400                         return imageBuffer->getWriteData()[idx];
00401                         }
00402 
00403                 inline Type operator()(const QPoint point, const uInt channel = 0) const
00404                         {
00405                         Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00406                         Q_ASSERT_X(point.x() < getCols(),"QVImage::operator()","col further upper bound");
00407                         Q_ASSERT_X(point.y() < getRows(),"QVImage::operator()","row further upper bound");
00408                         const int idx = step_div_type_size*point.y() + Channels*point.x() + channel;
00409                         Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00410                         Q_ASSERT_X(idx < getDataSize(),"QVImage::operator()","accessing above data");
00411 
00412                         return imageBuffer->getReadData()[idx];
00413                         }
00414 
00423                 QVImage<Type, 1> operator()(const uInt channel = 0) const;
00424 
00441                 QVImage<Type, Channels> & operator=(const QVImage<uChar, 1> &sourceImage);
00442 
00444                 QVImage<Type, Channels> & operator=(const QVImage<uChar, 3> &sourceImage);
00445 
00447                 QVImage<Type, Channels> & operator=(const QVImage<uShort, 1> &sourceImage);
00448 
00450                 QVImage<Type, Channels> & operator=(const QVImage<uShort, 3> &sourceImage);
00451 
00453                 QVImage<Type, Channels> & operator=(const QVImage<sShort, 1> &sourceImage);
00454 
00456                 QVImage<Type, Channels> & operator=(const QVImage<sShort, 3> &sourceImage);
00457 
00459                 QVImage<Type, Channels> & operator=(const QVImage<sInt, 1> &sourceImage);
00460 
00462                 QVImage<Type, Channels> & operator=(const QVImage<sInt, 3> &sourceImage);
00463 
00465                 QVImage<Type, Channels> & operator=(const QVImage<sFloat, 1> &sourceImage);
00466 
00468                 QVImage<Type, Channels> & operator=(const QVImage<sFloat, 3> &sourceImage);
00469 
00480                 bool operator==(const QVImage<Type, Channels> &img) const;
00481 
00495                 bool operator!=(const QVImage<Type, Channels> &img) const { return !(*this == img); }
00496 
00502                 QVImage<uChar, 1> operator<(const QVImage<uChar, Channels> &img) const;
00503 
00509                 QVImage<uChar, 1> operator<(const QVImage<uShort, Channels> &img) const;
00510 
00516                 QVImage<uChar, 1> operator<(const QVImage<sShort, Channels> &img) const;
00517 
00523                 QVImage<uChar, 1> operator<(const QVImage<sInt, Channels> &img) const;
00524 
00530                 QVImage<uChar, 1> operator<(const QVImage<sFloat, Channels> &img) const;
00531 
00537                 QVImage<uChar, 1> operator>(const QVImage<uChar, Channels> &img) const;
00538 
00544                 QVImage<uChar, 1> operator>(const QVImage<uShort, Channels> &img) const;
00545 
00551                 QVImage<uChar, 1> operator>(const QVImage<sShort, Channels> &img) const;
00552 
00558                 QVImage<uChar, 1> operator>(const QVImage<sInt, Channels> &img) const;
00559 
00565                 QVImage<uChar, 1> operator>(const QVImage<sFloat, Channels> &img) const;
00566 
00572                 QVImage<uChar, 1> operator<=(const QVImage<uChar, Channels> &img) const;
00573 
00579                 QVImage<uChar, 1> operator<=(const QVImage<uShort, Channels> &img) const;
00580 
00586                 QVImage<uChar, 1> operator<=(const QVImage<sShort, Channels> &img) const;
00587 
00593                 QVImage<uChar, 1> operator<=(const QVImage<sInt, Channels> &img) const;
00594 
00600                 QVImage<uChar, 1> operator<=(const QVImage<sFloat, Channels> &img) const;
00601 
00607                 QVImage<uChar, 1> operator>=(const QVImage<uChar, Channels> &img) const;
00608 
00614                 QVImage<uChar, 1> operator>=(const QVImage<uShort, Channels> &img) const;
00615 
00621                 QVImage<uChar, 1> operator>=(const QVImage<sShort, Channels> &img) const;
00622 
00628                 QVImage<uChar, 1> operator>=(const QVImage<sInt, Channels> &img) const;
00629 
00635                 QVImage<uChar, 1> operator>=(const QVImage<sFloat, Channels> &img) const;
00636 
00646                 QVImage<Type, Channels> operator+(const Type constant) const;
00647 
00657                 QVImage<Type, Channels> operator*(const Type constant) const;
00658 
00668                 QVImage<Type, Channels> operator-(const Type constant) const;
00669 
00679                 QVImage<Type, Channels> operator/(const Type constant) const;
00680 
00690                 QVImage<Type, Channels> operator<<(const Type constant) const;
00691 
00701                 QVImage<Type, Channels> operator>>(const Type constant) const;
00702 
00708                 QVImage<Type, Channels> operator!() const;
00709 
00716                 QVImage<Type, Channels> operator&(const Type constant) const;
00717 
00724                 QVImage<Type, Channels> operator|(const Type constant) const;
00725 
00732                 QVImage<Type, Channels> operator^(const Type constant) const;
00733 
00741                 QVImage<Type, Channels> operator+(const QVImage<Type, Channels> &img) const;
00742 
00750                 QVImage<Type, Channels> operator*(const QVImage<Type, Channels> &img) const;
00751 
00759                 QVImage<Type, Channels> operator-(const QVImage<Type, Channels> &img) const;
00760 
00768                 QVImage<Type, Channels> operator/(const QVImage<Type, Channels> &img) const;
00769         };
00770 
00771 template <typename Type, int C> bool QVImage<Type, C>::operator==(const QVImage<Type, C> &img) const
00772         {
00773         Q_ASSERT_X(img.getChannels() == this->getChannels(), "QVImage::operator==", "different number of planes");
00774         if (this->getCols() != img.getCols()) return false;
00775         if (this->getRows() != img.getRows()) return false;
00776         if (this->getChannels() != img.getChannels()) return false;
00777         if (this->getROI() != img.getROI()) return false;
00778         QVIMAGE_INIT_READ(Type,img);
00779         QVIMAGE_PTR_INIT_READ(Type,this);
00780 
00781         uInt x0 = (uInt) img.getROI().x(), y0 = (uInt) img.getROI().y(); 
00782         uInt x1 = (uInt) img.getROI().width(), y1 = (uInt) img.getROI().height(); 
00783 
00784         uInt lineSize = x1 * img.getChannels();
00785         for(uInt row = y0; row < y1; row++)
00786                 if (memcmp(&QVIMAGE_PIXEL(img, x0, row, 0),&QVIMAGE_PIXEL(this, x0, row, 0), lineSize) != 0)
00787                         return false;   
00788         return true;
00789         };
00790 
00791 
00792 typedef QVImage<uChar,1> QVImageUCharC1;
00793 typedef QVImage<uChar,3> QVImageUCharC3;
00794 typedef QVImage<uShort,1> QVImageUShortC1;
00795 typedef QVImage<uShort,3> QVImageUShortC3;
00796 typedef QVImage<sShort,1> QVImageSShortC1;
00797 typedef QVImage<sShort,3> QVImageSShortC3;
00798 typedef QVImage<sInt,1> QVImageSIntC1;
00799 typedef QVImage<sInt,3> QVImageSIntC3;
00800 typedef QVImage<sFloat,1> QVImageSFloatC1;
00801 typedef QVImage<sFloat,3> QVImageSFloatC3;
00802 
00803 Q_DECLARE_METATYPE(QVImageUCharC1);
00804 Q_DECLARE_METATYPE(QVImageUCharC3);
00805 Q_DECLARE_METATYPE(QVImageUShortC1);
00806 Q_DECLARE_METATYPE(QVImageUShortC3);
00807 Q_DECLARE_METATYPE(QVImageSShortC1);
00808 Q_DECLARE_METATYPE(QVImageSShortC3);
00809 Q_DECLARE_METATYPE(QVImageSIntC1);
00810 Q_DECLARE_METATYPE(QVImageSIntC3);
00811 Q_DECLARE_METATYPE(QVImageSFloatC1);
00812 Q_DECLARE_METATYPE(QVImageSFloatC3);
00813 
00814 #endif // QVIMAGE_H



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