00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #ifndef QVIMAGE_H
00026 #define QVIMAGE_H
00027
00028 #include <QMetaType>
00029 #include <QVGenericImage>
00030 #include <qvip/qvimagebuffer.h>
00031 #include <QImage>
00032
00033 #ifdef OPENCV
00034 #include <cv.h>
00035 #endif
00036
00049 #define QVIMAGE_INIT_READ(TYPE, IMAGE) \
00050 const TYPE * __qv_data_##IMAGE##__ = IMAGE.getReadData(); \
00051 const uInt __qv_step_##IMAGE##__ = IMAGE.getStep()/sizeof(TYPE); \
00052 const uChar __qv_planes_##IMAGE##__ = IMAGE.getChannels(); \
00053 const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - IMAGE.getCols(); \
00054 Q_UNUSED (__qv_next_line_inc_##IMAGE##__) ;
00055
00068 #define QVIMAGE_INIT_WRITE(TYPE, IMAGE) \
00069 TYPE * __qv_data_##IMAGE##__ = IMAGE.getWriteData(); \
00070 const uInt __qv_step_##IMAGE##__ = IMAGE.getStep()/sizeof(TYPE); \
00071 const uChar __qv_planes_##IMAGE##__ = IMAGE.getChannels(); \
00072 const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - IMAGE.getCols(); \
00073 Q_UNUSED (__qv_next_line_inc_##IMAGE##__) ;
00074
00087 #define QVIMAGE_PTR_INIT_READ(TYPE, IMAGE) \
00088 const TYPE * __qv_data_##IMAGE##__ = IMAGE->getReadData(); \
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##__ - IMAGE->getCols(); \
00092 Q_UNUSED (__qv_next_line_inc_##IMAGE##__) ;
00093
00106 #define QVIMAGE_PTR_INIT_WRITE(TYPE, IMAGE) \
00107 TYPE * __qv_data_##IMAGE##__ = IMAGE->getWriteData(); \
00108 const uInt __qv_step_##IMAGE##__ = IMAGE->getStep()/sizeof(TYPE); \
00109 const uChar __qv_planes_##IMAGE##__ = IMAGE->getChannels(); \
00110 const uInt __qv_next_line_inc_##IMAGE##__ = __qv_step_##IMAGE##__ - __qv_planes_##IMAGE##__ * IMAGE->getCols(); \
00111 Q_UNUSED (__qv_next_line_inc_##IMAGE##__) ;
00112
00125 #define QVIMAGE_PIXEL(IMAGE, Col, Row, Channel) \
00126 (__qv_data_##IMAGE##__ [(Row)* __qv_step_##IMAGE##__ + __qv_planes_##IMAGE##__ *(Col)+(Channel)])
00127
00140 #define QVIMAGE_PIXEL_PTR(IMAGE, Col, Row, Channel) \
00141 (& (__qv_data_##IMAGE##__ [(Row)* __qv_step_##IMAGE##__ + __qv_planes_##IMAGE##__ *(Col)+(Channel)]))
00142
00152 #define QVIMAGE_ROW_INCREMENT_PTR(IMAGE) ( __qv_step_##IMAGE##__ )
00153
00164 #define QVIMAGE_COL_INCREMENT_PTR(IMAGE) ( __qv_planes_##IMAGE##__ )
00165
00175 #define QVIMAGE_NEXT_LINE_INCREMENT_PTR(IMAGE) ( __qv_next_line_inc_##IMAGE##__ )
00176
00187 #include <stdio.h>
00188 #include <stdlib.h>
00189 #include <iostream>
00190
00191 template <typename Type, int Channels = 1> class QVImage: public QVGenericImage
00192 {
00193 protected:
00194 uInt step_div_type_size;
00195 QSharedDataPointer< QVImageBuffer<Type,Channels> > imageBuffer;
00196
00197 public:
00203 QVImage():QVGenericImage()
00204 {
00205 this->imageBuffer = new QVImageBuffer<Type, Channels>(1, 1);
00206 setROI(0,0, this->imageBuffer->getCols(), this->imageBuffer->getRows());
00207 setAnchor(0,0);
00208 this->step_div_type_size = getStep()/sizeof(Type);
00209 }
00210
00247 QVImage(uInt cols, uInt rows, uInt step = 0, const Type * buffer = NULL):QVGenericImage()
00248 {
00249 this->imageBuffer = new QVImageBuffer<Type, Channels>(cols, rows, step, buffer);
00250 setROI(0,0, cols, rows);
00251 setAnchor(0,0);
00252 this->step_div_type_size = getStep()/sizeof(Type);
00253 }
00254
00271 QVImage(QVImage<uChar,1> const &img);
00272
00274 QVImage(QVImage<uChar,3> const &img);
00275
00277 QVImage(QVImage<uShort,1> const &img);
00278
00280 QVImage(QVImage<uShort,3> const &img);
00281
00283 QVImage(QVImage<sShort,1> const &img);
00284
00286 QVImage(QVImage<sShort,3> const &img);
00287
00289 QVImage(QVImage<sInt,1> const &img);
00290
00292 QVImage(QVImage<sInt,3> const &img);
00293
00295 QVImage(QVImage<sFloat,1> const &img);
00296
00298 QVImage(QVImage<sFloat,3> const &img);
00299
00301 QVImage(const QString &filename)
00302 {
00303 QImage qimg;
00304 qimg.load(filename);
00305 *this = QVImage<uChar, 3>(qimg);
00306 };
00307
00317 QVImage(QVImage<uChar,1> const &red, QVImage<uChar,1> const &green, QVImage<uChar,1> const &blue);
00318
00320 QVImage(QVImage<uShort,1> const &red, QVImage<uShort,1> const &green, QVImage<uShort,1> const &blue);
00321
00323 QVImage(QVImage<sShort,1> const &red, QVImage<sShort,1> const &green, QVImage<sShort,1> const &blue);
00324
00326 QVImage(QVImage<sInt,1> const &red, QVImage<sInt,1> const &green, QVImage<sInt,1> const &blue);
00327
00329 QVImage(QVImage<sFloat,1> const &red, QVImage<sFloat,1> const &green, QVImage<sFloat,1> const &blue);
00330
00331
00333 QVImage(const QImage &qImage);
00334
00336 operator QImage() const;
00337
00338
00339 #ifdef OPENCV
00347 QVImage(const IplImage *iplImage);
00348
00355 operator IplImage *() const;
00356 #endif
00357
00358
00360 const char * getTypeQString() const;
00361
00363 uInt getRows() const { return imageBuffer->getRows(); }
00364
00366 uInt getCols() const { return imageBuffer->getCols(); }
00367
00369 inline uInt getStep() const { return imageBuffer->getStep(); }
00370
00372 inline uInt getChannels() const { return imageBuffer->getChannels(); }
00373
00375 uInt getDataSize() const { return imageBuffer->getDataSize(); }
00376
00378 uInt getTypeSize() const { return imageBuffer->getTypeSize(); }
00379
00390 const Type * getReadData() const { return imageBuffer->getReadData(); }
00391
00402 Type * getWriteData() { return imageBuffer->getWriteData(); }
00403
00418 void set(Type c1 = 0, Type c2 = 0, Type c3 = 0);
00419
00420
00427 void resize(const int cols, const int rows)
00428 {
00429 if (((int)getCols()) >= cols && ((int)getRows()) >= rows)
00430 return;
00431
00432 QVImage<Type, Channels> temp(MAX(cols, (int)getCols()), MAX(rows, (int)getRows()));
00433 Copy(*this, temp);
00434
00435 *this = temp;
00436 }
00437
00446 inline Type &operator()(const uInt col, const uInt row, const uInt channel = 0)
00447 {
00448 Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00449 Q_ASSERT_X(col < getCols(),"QVImage::operator()","col further upper bound");
00450 Q_ASSERT_X(row < getRows(),"QVImage::operator()","row further upper bound");
00451 const int idx = step_div_type_size*row + Channels*col + channel;
00452 Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00453 Q_ASSERT_X((uint)idx < getDataSize(),"QVImage::operator()","accessing above data");
00454
00455 return imageBuffer->getWriteData()[idx];
00456 }
00457
00458 inline Type operator()(const uInt col, const uInt row, const uInt channel = 0) const
00459 {
00460 Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00461 Q_ASSERT_X(col < getCols(),"QVImage::operator()","col further upper bound");
00462 Q_ASSERT_X(row < getRows(),"QVImage::operator()","row further upper bound");
00463 const int idx = step_div_type_size*row + Channels*col + channel;
00464 Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00465 Q_ASSERT_X((uint)idx < getDataSize(),"QVImage::operator()","accessing above data");
00466
00467 return imageBuffer->getReadData()[idx];
00468 }
00469
00476 inline Type &operator()(const QPoint point, const uInt channel = 0)
00477 {
00478 Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00479 Q_ASSERT_X(point.x() < getCols(),"QVImage::operator()","col further upper bound");
00480 Q_ASSERT_X(point.y() < getRows(),"QVImage::operator()","row further upper bound");
00481 const int idx = step_div_type_size*point.y() + Channels*point.x() + channel;
00482 Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00483 Q_ASSERT_X((uint)idx < getDataSize(),"QVImage::operator()","accessing above data");
00484
00485 return imageBuffer->getWriteData()[idx];
00486 }
00487
00488 inline Type operator()(const QPoint point, const uInt channel = 0) const
00489 {
00490 Q_ASSERT_X(step_div_type_size == getStep()/sizeof(Type), "QVImage::operator()", "step/size(Type) incorrect");
00491 Q_ASSERT_X(point.x() < getCols(),"QVImage::operator()","col further upper bound");
00492 Q_ASSERT_X(point.y() < getRows(),"QVImage::operator()","row further upper bound");
00493 const int idx = step_div_type_size*point.y() + Channels*point.x() + channel;
00494 Q_ASSERT_X(idx >= 0,"QVImage::operator()","accessing below data");
00495 Q_ASSERT_X((uint)idx < getDataSize(),"QVImage::operator()","accessing above data");
00496
00497 return imageBuffer->getReadData()[idx];
00498 }
00499
00508 QVImage<Type, 1> operator()(const uInt channel = 0) const;
00509
00526 QVImage<Type, Channels> & operator=(const QVImage<uChar, 1> &sourceImage);
00527
00529 QVImage<Type, Channels> & operator=(const QVImage<uChar, 3> &sourceImage);
00530
00532 QVImage<Type, Channels> & operator=(const QVImage<uShort, 1> &sourceImage);
00533
00535 QVImage<Type, Channels> & operator=(const QVImage<uShort, 3> &sourceImage);
00536
00538 QVImage<Type, Channels> & operator=(const QVImage<sShort, 1> &sourceImage);
00539
00541 QVImage<Type, Channels> & operator=(const QVImage<sShort, 3> &sourceImage);
00542
00544 QVImage<Type, Channels> & operator=(const QVImage<sInt, 1> &sourceImage);
00545
00547 QVImage<Type, Channels> & operator=(const QVImage<sInt, 3> &sourceImage);
00548
00550 QVImage<Type, Channels> & operator=(const QVImage<sFloat, 1> &sourceImage);
00551
00553 QVImage<Type, Channels> & operator=(const QVImage<sFloat, 3> &sourceImage);
00554
00565 bool operator==(const QVImage<Type, Channels> &img) const;
00566
00580 bool operator!=(const QVImage<Type, Channels> &img) const { return !(*this == img); }
00581
00587 QVImage<uChar, 1> operator<(const QVImage<uChar, Channels> &img) const;
00588
00594 QVImage<uChar, 1> operator<(const QVImage<uShort, Channels> &img) const;
00595
00601 QVImage<uChar, 1> operator<(const QVImage<sShort, Channels> &img) const;
00602
00608 QVImage<uChar, 1> operator<(const QVImage<sInt, Channels> &img) const;
00609
00615 QVImage<uChar, 1> operator<(const QVImage<sFloat, Channels> &img) const;
00616
00622 QVImage<uChar, 1> operator>(const QVImage<uChar, Channels> &img) const;
00623
00629 QVImage<uChar, 1> operator>(const QVImage<uShort, Channels> &img) const;
00630
00636 QVImage<uChar, 1> operator>(const QVImage<sShort, Channels> &img) const;
00637
00643 QVImage<uChar, 1> operator>(const QVImage<sInt, Channels> &img) const;
00644
00650 QVImage<uChar, 1> operator>(const QVImage<sFloat, Channels> &img) const;
00651
00657 QVImage<uChar, 1> operator<=(const QVImage<uChar, Channels> &img) const;
00658
00664 QVImage<uChar, 1> operator<=(const QVImage<uShort, Channels> &img) const;
00665
00671 QVImage<uChar, 1> operator<=(const QVImage<sShort, Channels> &img) const;
00672
00678 QVImage<uChar, 1> operator<=(const QVImage<sInt, Channels> &img) const;
00679
00685 QVImage<uChar, 1> operator<=(const QVImage<sFloat, Channels> &img) const;
00686
00692 QVImage<uChar, 1> operator>=(const QVImage<uChar, Channels> &img) const;
00693
00699 QVImage<uChar, 1> operator>=(const QVImage<uShort, Channels> &img) const;
00700
00706 QVImage<uChar, 1> operator>=(const QVImage<sShort, Channels> &img) const;
00707
00713 QVImage<uChar, 1> operator>=(const QVImage<sInt, Channels> &img) const;
00714
00720 QVImage<uChar, 1> operator>=(const QVImage<sFloat, Channels> &img) const;
00721
00731 QVImage<Type, Channels> operator+(const Type constant) const;
00732
00742 QVImage<Type, Channels> operator*(const Type constant) const;
00743
00753 QVImage<Type, Channels> operator-(const Type constant) const;
00754
00764 QVImage<Type, Channels> operator/(const Type constant) const;
00765
00775 QVImage<Type, Channels> operator<<(const Type constant) const;
00776
00786 QVImage<Type, Channels> operator>>(const Type constant) const;
00787
00793 QVImage<Type, Channels> operator!() const;
00794
00801 QVImage<Type, Channels> operator&(const Type constant) const;
00802
00809 QVImage<Type, Channels> operator|(const Type constant) const;
00810
00817 QVImage<Type, Channels> operator^(const Type constant) const;
00818
00826 QVImage<Type, Channels> operator+(const QVImage<Type, Channels> &img) const;
00827
00835 QVImage<Type, Channels> operator*(const QVImage<Type, Channels> &img) const;
00836
00844 QVImage<Type, Channels> operator-(const QVImage<Type, Channels> &img) const;
00845
00853 QVImage<Type, Channels> operator/(const QVImage<Type, Channels> &img) const;
00854 };
00855
00860 template <typename Type, int C> bool QVImage<Type, C>::operator==(const QVImage<Type, C> &img) const
00861 {
00862 Q_ASSERT_X(img.getChannels() == this->getChannels(), "QVImage::operator==", "different number of planes");
00863 if (this->getCols() != img.getCols()) return false;
00864 if (this->getRows() != img.getRows()) return false;
00865 if (this->getChannels() != img.getChannels()) return false;
00866 if (this->getROI() != img.getROI()) return false;
00867 QVIMAGE_INIT_READ(Type,img);
00868 QVIMAGE_PTR_INIT_READ(Type,this);
00869
00870 uInt x0 = (uInt) img.getROI().x(), y0 = (uInt) img.getROI().y();
00871 uInt x1 = (uInt) img.getROI().width(), y1 = (uInt) img.getROI().height();
00872
00873 uInt lineSize = x1 * img.getChannels();
00874 for(uInt row = y0; row < y1; row++)
00875 if (memcmp(&QVIMAGE_PIXEL(img, x0, row, 0),&QVIMAGE_PIXEL(this, x0, row, 0), lineSize) != 0)
00876 return false;
00877 return true;
00878 };
00879
00880 typedef QVImage<uChar,1> QVImageUCharC1;
00881 typedef QVImage<uChar,3> QVImageUCharC3;
00882 typedef QVImage<uShort,1> QVImageUShortC1;
00883 typedef QVImage<uShort,3> QVImageUShortC3;
00884 typedef QVImage<sShort,1> QVImageSShortC1;
00885 typedef QVImage<sShort,3> QVImageSShortC3;
00886 typedef QVImage<sInt,1> QVImageSIntC1;
00887 typedef QVImage<sInt,3> QVImageSIntC3;
00888 typedef QVImage<sFloat,1> QVImageSFloatC1;
00889 typedef QVImage<sFloat,3> QVImageSFloatC3;
00890
00891 Q_DECLARE_METATYPE(QVImageUCharC1);
00892 Q_DECLARE_METATYPE(QVImageUCharC3);
00893 Q_DECLARE_METATYPE(QVImageUShortC1);
00894 Q_DECLARE_METATYPE(QVImageUShortC3);
00895 Q_DECLARE_METATYPE(QVImageSShortC1);
00896 Q_DECLARE_METATYPE(QVImageSShortC3);
00897 Q_DECLARE_METATYPE(QVImageSIntC1);
00898 Q_DECLARE_METATYPE(QVImageSIntC3);
00899 Q_DECLARE_METATYPE(QVImageSFloatC1);
00900 Q_DECLARE_METATYPE(QVImageSFloatC3);
00901
00902 #endif // QVIMAGE_H