00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 #ifndef __OPENCV_CORE_WIMAGE_HPP__
00101 #define __OPENCV_CORE_WIMAGE_HPP__
00102
00103 #include "opencv2/core/core_c.h"
00104
00105 #ifdef __cplusplus
00106
00107 namespace cv {
00108
00109 template <typename T> class WImage;
00110 template <typename T> class WImageBuffer;
00111 template <typename T> class WImageView;
00112
00113 template<typename T, int C> class WImageC;
00114 template<typename T, int C> class WImageBufferC;
00115 template<typename T, int C> class WImageViewC;
00116
00117
00118 typedef WImage<uchar> WImage_b;
00119 typedef WImageView<uchar> WImageView_b;
00120 typedef WImageBuffer<uchar> WImageBuffer_b;
00121
00122 typedef WImageC<uchar, 1> WImage1_b;
00123 typedef WImageViewC<uchar, 1> WImageView1_b;
00124 typedef WImageBufferC<uchar, 1> WImageBuffer1_b;
00125
00126 typedef WImageC<uchar, 3> WImage3_b;
00127 typedef WImageViewC<uchar, 3> WImageView3_b;
00128 typedef WImageBufferC<uchar, 3> WImageBuffer3_b;
00129
00130 typedef WImage<float> WImage_f;
00131 typedef WImageView<float> WImageView_f;
00132 typedef WImageBuffer<float> WImageBuffer_f;
00133
00134 typedef WImageC<float, 1> WImage1_f;
00135 typedef WImageViewC<float, 1> WImageView1_f;
00136 typedef WImageBufferC<float, 1> WImageBuffer1_f;
00137
00138 typedef WImageC<float, 3> WImage3_f;
00139 typedef WImageViewC<float, 3> WImageView3_f;
00140 typedef WImageBufferC<float, 3> WImageBuffer3_f;
00141
00142
00143
00144 typedef WImage<short> WImage_16s;
00145 typedef WImageView<short> WImageView_16s;
00146 typedef WImageBuffer<short> WImageBuffer_16s;
00147
00148 typedef WImageC<short, 1> WImage1_16s;
00149 typedef WImageViewC<short, 1> WImageView1_16s;
00150 typedef WImageBufferC<short, 1> WImageBuffer1_16s;
00151
00152 typedef WImageC<short, 3> WImage3_16s;
00153 typedef WImageViewC<short, 3> WImageView3_16s;
00154 typedef WImageBufferC<short, 3> WImageBuffer3_16s;
00155
00156 typedef WImage<ushort> WImage_16u;
00157 typedef WImageView<ushort> WImageView_16u;
00158 typedef WImageBuffer<ushort> WImageBuffer_16u;
00159
00160 typedef WImageC<ushort, 1> WImage1_16u;
00161 typedef WImageViewC<ushort, 1> WImageView1_16u;
00162 typedef WImageBufferC<ushort, 1> WImageBuffer1_16u;
00163
00164 typedef WImageC<ushort, 3> WImage3_16u;
00165 typedef WImageViewC<ushort, 3> WImageView3_16u;
00166 typedef WImageBufferC<ushort, 3> WImageBuffer3_16u;
00167
00168
00169
00170
00171
00172
00173
00174 template<typename T>
00175 class WImage
00176 {
00177 public:
00178 typedef T BaseType;
00179
00180
00181
00182 virtual ~WImage() = 0;
00183
00184
00185 IplImage* Ipl() {return image_; }
00186 const IplImage* Ipl() const {return image_; }
00187 T* ImageData() { return reinterpret_cast<T*>(image_->imageData); }
00188 const T* ImageData() const {
00189 return reinterpret_cast<const T*>(image_->imageData);
00190 }
00191
00192 int Width() const {return image_->width; }
00193 int Height() const {return image_->height; }
00194
00195
00196 int WidthStep() const {return image_->widthStep; }
00197
00198 int Channels() const {return image_->nChannels; }
00199 int ChannelSize() const {return sizeof(T); }
00200
00201
00202 int PixelSize() const {return Channels() * ChannelSize(); }
00203
00204
00205
00206
00207 int Depth() const;
00208
00209 inline const T* Row(int r) const {
00210 return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep);
00211 }
00212
00213 inline T* Row(int r) {
00214 return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep);
00215 }
00216
00217
00218 inline T* operator() (int c, int r) {
00219 return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep) +
00220 c*Channels();
00221 }
00222
00223 inline const T* operator() (int c, int r) const {
00224 return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep) +
00225 c*Channels();
00226 }
00227
00228
00229 void CopyFrom(const WImage<T>& src) { cvCopy(src.Ipl(), image_); }
00230
00231
00232 void SetZero() { cvSetZero(image_); }
00233
00234
00235 WImageView<T> View(int c, int r, int width, int height);
00236
00237 protected:
00238
00239 WImage(const WImage&);
00240 void operator=(const WImage&);
00241
00242 explicit WImage(IplImage* img) : image_(img) {
00243 assert(!img || img->depth == Depth());
00244 }
00245
00246 void SetIpl(IplImage* image) {
00247 assert(!image || image->depth == Depth());
00248 image_ = image;
00249 }
00250
00251 IplImage* image_;
00252 };
00253
00254
00255
00256
00257
00258
00259 template<typename T, int C>
00260 class WImageC : public WImage<T>
00261 {
00262 public:
00263 typedef typename WImage<T>::BaseType BaseType;
00264 enum { kChannels = C };
00265
00266 explicit WImageC(IplImage* img) : WImage<T>(img) {
00267 assert(!img || img->nChannels == Channels());
00268 }
00269
00270
00271 WImageViewC<T, C> View(int c, int r, int width, int height);
00272
00273
00274 void CopyFrom(const WImageC<T, C>& src) {
00275 cvCopy(src.Ipl(), WImage<T>::image_);
00276 }
00277
00278
00279
00280 virtual ~WImageC() = 0;
00281
00282 int Channels() const {return C; }
00283
00284 protected:
00285
00286 WImageC(const WImageC&);
00287 void operator=(const WImageC&);
00288
00289 void SetIpl(IplImage* image) {
00290 assert(!image || image->depth == WImage<T>::Depth());
00291 WImage<T>::SetIpl(image);
00292 }
00293 };
00294
00295
00296
00297
00298
00299
00300
00301 template<typename T>
00302 class WImageBuffer : public WImage<T>
00303 {
00304 public:
00305 typedef typename WImage<T>::BaseType BaseType;
00306
00307
00308 WImageBuffer() : WImage<T>(0) {}
00309
00310 WImageBuffer(int width, int height, int nchannels) : WImage<T>(0) {
00311 Allocate(width, height, nchannels);
00312 }
00313
00314
00315
00316 explicit WImageBuffer(IplImage* img) : WImage<T>(img) {}
00317
00318
00319
00320 void Allocate(int width, int height, int nchannels);
00321
00322
00323 void SetIpl(IplImage* img) {
00324 ReleaseImage();
00325 WImage<T>::SetIpl(img);
00326 }
00327
00328
00329 void CloneFrom(const WImage<T>& src) {
00330 Allocate(src.Width(), src.Height(), src.Channels());
00331 CopyFrom(src);
00332 }
00333
00334 ~WImageBuffer() {
00335 ReleaseImage();
00336 }
00337
00338
00339 void ReleaseImage() {
00340 if (WImage<T>::image_) {
00341 IplImage* image = WImage<T>::image_;
00342 cvReleaseImage(&image);
00343 WImage<T>::SetIpl(0);
00344 }
00345 }
00346
00347 bool IsNull() const {return WImage<T>::image_ == NULL; }
00348
00349 private:
00350
00351 WImageBuffer(const WImageBuffer&);
00352 void operator=(const WImageBuffer&);
00353 };
00354
00355
00356
00357 template<typename T, int C>
00358 class WImageBufferC : public WImageC<T, C>
00359 {
00360 public:
00361 typedef typename WImage<T>::BaseType BaseType;
00362 enum { kChannels = C };
00363
00364
00365 WImageBufferC() : WImageC<T, C>(0) {}
00366
00367 WImageBufferC(int width, int height) : WImageC<T, C>(0) {
00368 Allocate(width, height);
00369 }
00370
00371
00372
00373 explicit WImageBufferC(IplImage* img) : WImageC<T, C>(img) {}
00374
00375
00376
00377 void Allocate(int width, int height);
00378
00379
00380 void SetIpl(IplImage* img) {
00381 ReleaseImage();
00382 WImageC<T, C>::SetIpl(img);
00383 }
00384
00385
00386 void CloneFrom(const WImageC<T, C>& src) {
00387 Allocate(src.Width(), src.Height());
00388 CopyFrom(src);
00389 }
00390
00391 ~WImageBufferC() {
00392 ReleaseImage();
00393 }
00394
00395
00396 void ReleaseImage() {
00397 if (WImage<T>::image_) {
00398 IplImage* image = WImage<T>::image_;
00399 cvReleaseImage(&image);
00400 WImageC<T, C>::SetIpl(0);
00401 }
00402 }
00403
00404 bool IsNull() const {return WImage<T>::image_ == NULL; }
00405
00406 private:
00407
00408 WImageBufferC(const WImageBufferC&);
00409 void operator=(const WImageBufferC&);
00410 };
00411
00412
00413
00414
00415
00416
00417
00418 template<typename T>
00419 class WImageView : public WImage<T>
00420 {
00421 public:
00422 typedef typename WImage<T>::BaseType BaseType;
00423
00424
00425
00426 WImageView(WImage<T>* img, int c, int r, int width, int height);
00427
00428
00429
00430 WImageView(T* data, int width, int height, int channels, int width_step = -1);
00431
00432
00433
00434 WImageView(IplImage* img) : WImage<T>(img) {}
00435
00436
00437 WImageView(const WImage<T>& img) : WImage<T>(0) {
00438 header_ = *(img.Ipl());
00439 WImage<T>::SetIpl(&header_);
00440 }
00441
00442 WImageView& operator=(const WImage<T>& img) {
00443 header_ = *(img.Ipl());
00444 WImage<T>::SetIpl(&header_);
00445 return *this;
00446 }
00447
00448 protected:
00449 IplImage header_;
00450 };
00451
00452
00453 template<typename T, int C>
00454 class WImageViewC : public WImageC<T, C>
00455 {
00456 public:
00457 typedef typename WImage<T>::BaseType BaseType;
00458 enum { kChannels = C };
00459
00460
00461 WImageViewC();
00462
00463 virtual ~WImageViewC() {}
00464
00465
00466
00467 WImageViewC(WImageC<T, C>* img,
00468 int c, int r, int width, int height);
00469
00470
00471 WImageViewC(T* data, int width, int height, int width_step = -1);
00472
00473
00474
00475 WImageViewC(IplImage* img) : WImageC<T, C>(img) {}
00476
00477
00478
00479
00480 WImageViewC(const WImageC<T, C>& img) : WImageC<T, C>(0) {
00481 header_ = *(img.Ipl());
00482 WImageC<T, C>::SetIpl(&header_);
00483 }
00484 WImageViewC(const WImageViewC<T, C>& img) : WImageC<T, C>(0) {
00485 header_ = *(img.Ipl());
00486 WImageC<T, C>::SetIpl(&header_);
00487 }
00488
00489 WImageViewC& operator=(const WImageC<T, C>& img) {
00490 header_ = *(img.Ipl());
00491 WImageC<T, C>::SetIpl(&header_);
00492 return *this;
00493 }
00494 WImageViewC& operator=(const WImageViewC<T, C>& img) {
00495 header_ = *(img.Ipl());
00496 WImageC<T, C>::SetIpl(&header_);
00497 return *this;
00498 }
00499
00500 protected:
00501 IplImage header_;
00502 };
00503
00504
00505
00506 template<>
00507 inline int WImage<uchar>::Depth() const {return IPL_DEPTH_8U; }
00508 template<>
00509 inline int WImage<signed char>::Depth() const {return IPL_DEPTH_8S; }
00510 template<>
00511 inline int WImage<short>::Depth() const {return IPL_DEPTH_16S; }
00512 template<>
00513 inline int WImage<ushort>::Depth() const {return IPL_DEPTH_16U; }
00514 template<>
00515 inline int WImage<int>::Depth() const {return IPL_DEPTH_32S; }
00516 template<>
00517 inline int WImage<float>::Depth() const {return IPL_DEPTH_32F; }
00518 template<>
00519 inline int WImage<double>::Depth() const {return IPL_DEPTH_64F; }
00520
00521
00522
00523
00524 template<typename T> inline WImage<T>::~WImage() {}
00525 template<typename T, int C> inline WImageC<T, C>::~WImageC() {}
00526
00527
00528
00529
00530 template<typename T>
00531 inline void WImageBuffer<T>::Allocate(int width, int height, int nchannels)
00532 {
00533 if (IsNull() || WImage<T>::Width() != width ||
00534 WImage<T>::Height() != height || WImage<T>::Channels() != nchannels) {
00535 ReleaseImage();
00536 WImage<T>::image_ = cvCreateImage(cvSize(width, height),
00537 WImage<T>::Depth(), nchannels);
00538 }
00539 }
00540
00541 template<typename T, int C>
00542 inline void WImageBufferC<T, C>::Allocate(int width, int height)
00543 {
00544 if (IsNull() || WImage<T>::Width() != width || WImage<T>::Height() != height) {
00545 ReleaseImage();
00546 WImageC<T, C>::SetIpl(cvCreateImage(cvSize(width, height),WImage<T>::Depth(), C));
00547 }
00548 }
00549
00550
00551
00552
00553 template<typename T>
00554 WImageView<T>::WImageView(WImage<T>* img, int c, int r, int width, int height)
00555 : WImage<T>(0)
00556 {
00557 header_ = *(img->Ipl());
00558 header_.imageData = reinterpret_cast<char*>((*img)(c, r));
00559 header_.width = width;
00560 header_.height = height;
00561 WImage<T>::SetIpl(&header_);
00562 }
00563
00564 template<typename T>
00565 WImageView<T>::WImageView(T* data, int width, int height, int nchannels, int width_step)
00566 : WImage<T>(0)
00567 {
00568 cvInitImageHeader(&header_, cvSize(width, height), WImage<T>::Depth(), nchannels);
00569 header_.imageData = reinterpret_cast<char*>(data);
00570 if (width_step > 0) {
00571 header_.widthStep = width_step;
00572 }
00573 WImage<T>::SetIpl(&header_);
00574 }
00575
00576 template<typename T, int C>
00577 WImageViewC<T, C>::WImageViewC(WImageC<T, C>* img, int c, int r, int width, int height)
00578 : WImageC<T, C>(0)
00579 {
00580 header_ = *(img->Ipl());
00581 header_.imageData = reinterpret_cast<char*>((*img)(c, r));
00582 header_.width = width;
00583 header_.height = height;
00584 WImageC<T, C>::SetIpl(&header_);
00585 }
00586
00587 template<typename T, int C>
00588 WImageViewC<T, C>::WImageViewC() : WImageC<T, C>(0) {
00589 cvInitImageHeader(&header_, cvSize(0, 0), WImage<T>::Depth(), C);
00590 header_.imageData = reinterpret_cast<char*>(0);
00591 WImageC<T, C>::SetIpl(&header_);
00592 }
00593
00594 template<typename T, int C>
00595 WImageViewC<T, C>::WImageViewC(T* data, int width, int height, int width_step)
00596 : WImageC<T, C>(0)
00597 {
00598 cvInitImageHeader(&header_, cvSize(width, height), WImage<T>::Depth(), C);
00599 header_.imageData = reinterpret_cast<char*>(data);
00600 if (width_step > 0) {
00601 header_.widthStep = width_step;
00602 }
00603 WImageC<T, C>::SetIpl(&header_);
00604 }
00605
00606
00607 template<typename T>
00608 WImageView<T> WImage<T>::View(int c, int r, int width, int height) {
00609 return WImageView<T>(this, c, r, width, height);
00610 }
00611
00612 template<typename T, int C>
00613 WImageViewC<T, C> WImageC<T, C>::View(int c, int r, int width, int height) {
00614 return WImageViewC<T, C>(this, c, r, width, height);
00615 }
00616
00617 }
00618
00619 #endif // __cplusplus
00620
00621 #endif