wimage.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*M//////////////////////////////////////////////////////////////////////////////
  2. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  3. //
  4. // By downloading, copying, installing or using the software you agree to
  5. // this license. If you do not agree to this license, do not download,
  6. // install, copy or use the software.
  7. //
  8. // License Agreement
  9. // For Open Source Computer Vision Library
  10. //
  11. // Copyright (C) 2008, Google, all rights reserved.
  12. // Third party copyrights are property of their respective owners.
  13. //
  14. // Redistribution and use in source and binary forms, with or without
  15. // modification, are permitted provided that the following conditions are met:
  16. //
  17. // * Redistribution's of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. //
  20. // * Redistribution's in binary form must reproduce the above copyright notice,
  21. // this list of conditions and the following disclaimer in the documentation
  22. // and/or other materials provided with the distribution.
  23. //
  24. // * The name of Intel Corporation or contributors may not be used to endorse
  25. // or promote products derived from this software without specific
  26. // prior written permission.
  27. //
  28. // This software is provided by the copyright holders and contributors "as is"
  29. // and any express or implied warranties, including, but not limited to, the
  30. // implied warranties of merchantability and fitness for a particular purpose
  31. // are disclaimed. In no event shall the Intel Corporation or contributors be
  32. // liable for any direct, indirect, incidental, special, exemplary, or
  33. // consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. /////////////////////////////////////////////////////////////////////////////////
  40. //M*/
  41. #ifndef OPENCV_CORE_WIMAGE_HPP
  42. #define OPENCV_CORE_WIMAGE_HPP
  43. #include "opencv2/core/core_c.h"
  44. #ifdef __cplusplus
  45. namespace cv {
  46. //! @addtogroup core
  47. //! @{
  48. template <typename T> class WImage;
  49. template <typename T> class WImageBuffer;
  50. template <typename T> class WImageView;
  51. template<typename T, int C> class WImageC;
  52. template<typename T, int C> class WImageBufferC;
  53. template<typename T, int C> class WImageViewC;
  54. // Commonly used typedefs.
  55. typedef WImage<uchar> WImage_b;
  56. typedef WImageView<uchar> WImageView_b;
  57. typedef WImageBuffer<uchar> WImageBuffer_b;
  58. typedef WImageC<uchar, 1> WImage1_b;
  59. typedef WImageViewC<uchar, 1> WImageView1_b;
  60. typedef WImageBufferC<uchar, 1> WImageBuffer1_b;
  61. typedef WImageC<uchar, 3> WImage3_b;
  62. typedef WImageViewC<uchar, 3> WImageView3_b;
  63. typedef WImageBufferC<uchar, 3> WImageBuffer3_b;
  64. typedef WImage<float> WImage_f;
  65. typedef WImageView<float> WImageView_f;
  66. typedef WImageBuffer<float> WImageBuffer_f;
  67. typedef WImageC<float, 1> WImage1_f;
  68. typedef WImageViewC<float, 1> WImageView1_f;
  69. typedef WImageBufferC<float, 1> WImageBuffer1_f;
  70. typedef WImageC<float, 3> WImage3_f;
  71. typedef WImageViewC<float, 3> WImageView3_f;
  72. typedef WImageBufferC<float, 3> WImageBuffer3_f;
  73. // There isn't a standard for signed and unsigned short so be more
  74. // explicit in the typename for these cases.
  75. typedef WImage<short> WImage_16s;
  76. typedef WImageView<short> WImageView_16s;
  77. typedef WImageBuffer<short> WImageBuffer_16s;
  78. typedef WImageC<short, 1> WImage1_16s;
  79. typedef WImageViewC<short, 1> WImageView1_16s;
  80. typedef WImageBufferC<short, 1> WImageBuffer1_16s;
  81. typedef WImageC<short, 3> WImage3_16s;
  82. typedef WImageViewC<short, 3> WImageView3_16s;
  83. typedef WImageBufferC<short, 3> WImageBuffer3_16s;
  84. typedef WImage<ushort> WImage_16u;
  85. typedef WImageView<ushort> WImageView_16u;
  86. typedef WImageBuffer<ushort> WImageBuffer_16u;
  87. typedef WImageC<ushort, 1> WImage1_16u;
  88. typedef WImageViewC<ushort, 1> WImageView1_16u;
  89. typedef WImageBufferC<ushort, 1> WImageBuffer1_16u;
  90. typedef WImageC<ushort, 3> WImage3_16u;
  91. typedef WImageViewC<ushort, 3> WImageView3_16u;
  92. typedef WImageBufferC<ushort, 3> WImageBuffer3_16u;
  93. /** @brief Image class which provides a thin layer around an IplImage.
  94. The goals of the class design are:
  95. -# All the data has explicit ownership to avoid memory leaks
  96. -# No hidden allocations or copies for performance.
  97. -# Easy access to OpenCV methods (which will access IPP if available)
  98. -# Can easily treat external data as an image
  99. -# Easy to create images which are subsets of other images
  100. -# Fast pixel access which can take advantage of number of channels if known at compile time.
  101. The WImage class is the image class which provides the data accessors. The 'W' comes from the fact
  102. that it is also a wrapper around the popular but inconvenient IplImage class. A WImage can be
  103. constructed either using a WImageBuffer class which allocates and frees the data, or using a
  104. WImageView class which constructs a subimage or a view into external data. The view class does no
  105. memory management. Each class actually has two versions, one when the number of channels is known
  106. at compile time and one when it isn't. Using the one with the number of channels specified can
  107. provide some compile time optimizations by using the fact that the number of channels is a
  108. constant.
  109. We use the convention (c,r) to refer to column c and row r with (0,0) being the upper left corner.
  110. This is similar to standard Euclidean coordinates with the first coordinate varying in the
  111. horizontal direction and the second coordinate varying in the vertical direction. Thus (c,r) is
  112. usually in the domain [0, width) X [0, height)
  113. Example usage:
  114. @code
  115. WImageBuffer3_b im(5,7); // Make a 5X7 3 channel image of type uchar
  116. WImageView3_b sub_im(im, 2,2, 3,3); // 3X3 submatrix
  117. vector<float> vec(10, 3.0f);
  118. WImageView1_f user_im(&vec[0], 2, 5); // 2X5 image w/ supplied data
  119. im.SetZero(); // same as cvSetZero(im.Ipl())
  120. *im(2, 3) = 15; // Modify the element at column 2, row 3
  121. MySetRand(&sub_im);
  122. // Copy the second row into the first. This can be done with no memory
  123. // allocation and will use SSE if IPP is available.
  124. int w = im.Width();
  125. im.View(0,0, w,1).CopyFrom(im.View(0,1, w,1));
  126. // Doesn't care about source of data since using WImage
  127. void MySetRand(WImage_b* im) { // Works with any number of channels
  128. for (int r = 0; r < im->Height(); ++r) {
  129. float* row = im->Row(r);
  130. for (int c = 0; c < im->Width(); ++c) {
  131. for (int ch = 0; ch < im->Channels(); ++ch, ++row) {
  132. *row = uchar(rand() & 255);
  133. }
  134. }
  135. }
  136. }
  137. @endcode
  138. Functions that are not part of the basic image allocation, viewing, and access should come from
  139. OpenCV, except some useful functions that are not part of OpenCV can be found in wimage_util.h
  140. */
  141. template<typename T>
  142. class WImage
  143. {
  144. public:
  145. typedef T BaseType;
  146. // WImage is an abstract class with no other virtual methods so make the
  147. // destructor virtual.
  148. virtual ~WImage() = 0;
  149. // Accessors
  150. IplImage* Ipl() {return image_; }
  151. const IplImage* Ipl() const {return image_; }
  152. T* ImageData() { return reinterpret_cast<T*>(image_->imageData); }
  153. const T* ImageData() const {
  154. return reinterpret_cast<const T*>(image_->imageData);
  155. }
  156. int Width() const {return image_->width; }
  157. int Height() const {return image_->height; }
  158. // WidthStep is the number of bytes to go to the pixel with the next y coord
  159. int WidthStep() const {return image_->widthStep; }
  160. int Channels() const {return image_->nChannels; }
  161. int ChannelSize() const {return sizeof(T); } // number of bytes per channel
  162. // Number of bytes per pixel
  163. int PixelSize() const {return Channels() * ChannelSize(); }
  164. // Return depth type (e.g. IPL_DEPTH_8U, IPL_DEPTH_32F) which is the number
  165. // of bits per channel and with the signed bit set.
  166. // This is known at compile time using specializations.
  167. int Depth() const;
  168. inline const T* Row(int r) const {
  169. return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep);
  170. }
  171. inline T* Row(int r) {
  172. return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep);
  173. }
  174. // Pixel accessors which returns a pointer to the start of the channel
  175. inline T* operator() (int c, int r) {
  176. return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep) +
  177. c*Channels();
  178. }
  179. inline const T* operator() (int c, int r) const {
  180. return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep) +
  181. c*Channels();
  182. }
  183. // Copy the contents from another image which is just a convenience to cvCopy
  184. void CopyFrom(const WImage<T>& src) { cvCopy(src.Ipl(), image_); }
  185. // Set contents to zero which is just a convenient to cvSetZero
  186. void SetZero() { cvSetZero(image_); }
  187. // Construct a view into a region of this image
  188. WImageView<T> View(int c, int r, int width, int height);
  189. protected:
  190. // Disallow copy and assignment
  191. WImage(const WImage&);
  192. void operator=(const WImage&);
  193. explicit WImage(IplImage* img) : image_(img) {
  194. assert(!img || img->depth == Depth());
  195. }
  196. void SetIpl(IplImage* image) {
  197. assert(!image || image->depth == Depth());
  198. image_ = image;
  199. }
  200. IplImage* image_;
  201. };
  202. /** Image class when both the pixel type and number of channels
  203. are known at compile time. This wrapper will speed up some of the operations
  204. like accessing individual pixels using the () operator.
  205. */
  206. template<typename T, int C>
  207. class WImageC : public WImage<T>
  208. {
  209. public:
  210. typedef typename WImage<T>::BaseType BaseType;
  211. enum { kChannels = C };
  212. explicit WImageC(IplImage* img) : WImage<T>(img) {
  213. assert(!img || img->nChannels == Channels());
  214. }
  215. // Construct a view into a region of this image
  216. WImageViewC<T, C> View(int c, int r, int width, int height);
  217. // Copy the contents from another image which is just a convenience to cvCopy
  218. void CopyFrom(const WImageC<T, C>& src) {
  219. cvCopy(src.Ipl(), WImage<T>::image_);
  220. }
  221. // WImageC is an abstract class with no other virtual methods so make the
  222. // destructor virtual.
  223. virtual ~WImageC() = 0;
  224. int Channels() const {return C; }
  225. protected:
  226. // Disallow copy and assignment
  227. WImageC(const WImageC&);
  228. void operator=(const WImageC&);
  229. void SetIpl(IplImage* image) {
  230. assert(!image || image->depth == WImage<T>::Depth());
  231. WImage<T>::SetIpl(image);
  232. }
  233. };
  234. /** Image class which owns the data, so it can be allocated and is always
  235. freed. It cannot be copied but can be explicity cloned.
  236. */
  237. template<typename T>
  238. class WImageBuffer : public WImage<T>
  239. {
  240. public:
  241. typedef typename WImage<T>::BaseType BaseType;
  242. // Default constructor which creates an object that can be
  243. WImageBuffer() : WImage<T>(0) {}
  244. WImageBuffer(int width, int height, int nchannels) : WImage<T>(0) {
  245. Allocate(width, height, nchannels);
  246. }
  247. // Constructor which takes ownership of a given IplImage so releases
  248. // the image on destruction.
  249. explicit WImageBuffer(IplImage* img) : WImage<T>(img) {}
  250. // Allocate an image. Does nothing if current size is the same as
  251. // the new size.
  252. void Allocate(int width, int height, int nchannels);
  253. // Set the data to point to an image, releasing the old data
  254. void SetIpl(IplImage* img) {
  255. ReleaseImage();
  256. WImage<T>::SetIpl(img);
  257. }
  258. // Clone an image which reallocates the image if of a different dimension.
  259. void CloneFrom(const WImage<T>& src) {
  260. Allocate(src.Width(), src.Height(), src.Channels());
  261. CopyFrom(src);
  262. }
  263. ~WImageBuffer() {
  264. ReleaseImage();
  265. }
  266. // Release the image if it isn't null.
  267. void ReleaseImage() {
  268. if (WImage<T>::image_) {
  269. IplImage* image = WImage<T>::image_;
  270. cvReleaseImage(&image);
  271. WImage<T>::SetIpl(0);
  272. }
  273. }
  274. bool IsNull() const {return WImage<T>::image_ == NULL; }
  275. private:
  276. // Disallow copy and assignment
  277. WImageBuffer(const WImageBuffer&);
  278. void operator=(const WImageBuffer&);
  279. };
  280. /** Like a WImageBuffer class but when the number of channels is known at compile time.
  281. */
  282. template<typename T, int C>
  283. class WImageBufferC : public WImageC<T, C>
  284. {
  285. public:
  286. typedef typename WImage<T>::BaseType BaseType;
  287. enum { kChannels = C };
  288. // Default constructor which creates an object that can be
  289. WImageBufferC() : WImageC<T, C>(0) {}
  290. WImageBufferC(int width, int height) : WImageC<T, C>(0) {
  291. Allocate(width, height);
  292. }
  293. // Constructor which takes ownership of a given IplImage so releases
  294. // the image on destruction.
  295. explicit WImageBufferC(IplImage* img) : WImageC<T, C>(img) {}
  296. // Allocate an image. Does nothing if current size is the same as
  297. // the new size.
  298. void Allocate(int width, int height);
  299. // Set the data to point to an image, releasing the old data
  300. void SetIpl(IplImage* img) {
  301. ReleaseImage();
  302. WImageC<T, C>::SetIpl(img);
  303. }
  304. // Clone an image which reallocates the image if of a different dimension.
  305. void CloneFrom(const WImageC<T, C>& src) {
  306. Allocate(src.Width(), src.Height());
  307. CopyFrom(src);
  308. }
  309. ~WImageBufferC() {
  310. ReleaseImage();
  311. }
  312. // Release the image if it isn't null.
  313. void ReleaseImage() {
  314. if (WImage<T>::image_) {
  315. IplImage* image = WImage<T>::image_;
  316. cvReleaseImage(&image);
  317. WImageC<T, C>::SetIpl(0);
  318. }
  319. }
  320. bool IsNull() const {return WImage<T>::image_ == NULL; }
  321. private:
  322. // Disallow copy and assignment
  323. WImageBufferC(const WImageBufferC&);
  324. void operator=(const WImageBufferC&);
  325. };
  326. /** View into an image class which allows treating a subimage as an image or treating external data
  327. as an image
  328. */
  329. template<typename T> class WImageView : public WImage<T>
  330. {
  331. public:
  332. typedef typename WImage<T>::BaseType BaseType;
  333. // Construct a subimage. No checks are done that the subimage lies
  334. // completely inside the original image.
  335. WImageView(WImage<T>* img, int c, int r, int width, int height);
  336. // Refer to external data.
  337. // If not given width_step assumed to be same as width.
  338. WImageView(T* data, int width, int height, int channels, int width_step = -1);
  339. // Refer to external data. This does NOT take ownership
  340. // of the supplied IplImage.
  341. WImageView(IplImage* img) : WImage<T>(img) {}
  342. // Copy constructor
  343. WImageView(const WImage<T>& img) : WImage<T>(0) {
  344. header_ = *(img.Ipl());
  345. WImage<T>::SetIpl(&header_);
  346. }
  347. WImageView& operator=(const WImage<T>& img) {
  348. header_ = *(img.Ipl());
  349. WImage<T>::SetIpl(&header_);
  350. return *this;
  351. }
  352. protected:
  353. IplImage header_;
  354. };
  355. template<typename T, int C>
  356. class WImageViewC : public WImageC<T, C>
  357. {
  358. public:
  359. typedef typename WImage<T>::BaseType BaseType;
  360. enum { kChannels = C };
  361. // Default constructor needed for vectors of views.
  362. WImageViewC();
  363. virtual ~WImageViewC() {}
  364. // Construct a subimage. No checks are done that the subimage lies
  365. // completely inside the original image.
  366. WImageViewC(WImageC<T, C>* img,
  367. int c, int r, int width, int height);
  368. // Refer to external data
  369. WImageViewC(T* data, int width, int height, int width_step = -1);
  370. // Refer to external data. This does NOT take ownership
  371. // of the supplied IplImage.
  372. WImageViewC(IplImage* img) : WImageC<T, C>(img) {}
  373. // Copy constructor which does a shallow copy to allow multiple views
  374. // of same data. gcc-4.1.1 gets confused if both versions of
  375. // the constructor and assignment operator are not provided.
  376. WImageViewC(const WImageC<T, C>& img) : WImageC<T, C>(0) {
  377. header_ = *(img.Ipl());
  378. WImageC<T, C>::SetIpl(&header_);
  379. }
  380. WImageViewC(const WImageViewC<T, C>& img) : WImageC<T, C>(0) {
  381. header_ = *(img.Ipl());
  382. WImageC<T, C>::SetIpl(&header_);
  383. }
  384. WImageViewC& operator=(const WImageC<T, C>& img) {
  385. header_ = *(img.Ipl());
  386. WImageC<T, C>::SetIpl(&header_);
  387. return *this;
  388. }
  389. WImageViewC& operator=(const WImageViewC<T, C>& img) {
  390. header_ = *(img.Ipl());
  391. WImageC<T, C>::SetIpl(&header_);
  392. return *this;
  393. }
  394. protected:
  395. IplImage header_;
  396. };
  397. // Specializations for depth
  398. template<>
  399. inline int WImage<uchar>::Depth() const {return IPL_DEPTH_8U; }
  400. template<>
  401. inline int WImage<signed char>::Depth() const {return IPL_DEPTH_8S; }
  402. template<>
  403. inline int WImage<short>::Depth() const {return IPL_DEPTH_16S; }
  404. template<>
  405. inline int WImage<ushort>::Depth() const {return IPL_DEPTH_16U; }
  406. template<>
  407. inline int WImage<int>::Depth() const {return IPL_DEPTH_32S; }
  408. template<>
  409. inline int WImage<float>::Depth() const {return IPL_DEPTH_32F; }
  410. template<>
  411. inline int WImage<double>::Depth() const {return IPL_DEPTH_64F; }
  412. template<typename T> inline WImage<T>::~WImage() {}
  413. template<typename T, int C> inline WImageC<T, C>::~WImageC() {}
  414. template<typename T>
  415. inline void WImageBuffer<T>::Allocate(int width, int height, int nchannels)
  416. {
  417. if (IsNull() || WImage<T>::Width() != width ||
  418. WImage<T>::Height() != height || WImage<T>::Channels() != nchannels) {
  419. ReleaseImage();
  420. WImage<T>::image_ = cvCreateImage(cvSize(width, height),
  421. WImage<T>::Depth(), nchannels);
  422. }
  423. }
  424. template<typename T, int C>
  425. inline void WImageBufferC<T, C>::Allocate(int width, int height)
  426. {
  427. if (IsNull() || WImage<T>::Width() != width || WImage<T>::Height() != height) {
  428. ReleaseImage();
  429. WImageC<T, C>::SetIpl(cvCreateImage(cvSize(width, height),WImage<T>::Depth(), C));
  430. }
  431. }
  432. template<typename T>
  433. WImageView<T>::WImageView(WImage<T>* img, int c, int r, int width, int height)
  434. : WImage<T>(0)
  435. {
  436. header_ = *(img->Ipl());
  437. header_.imageData = reinterpret_cast<char*>((*img)(c, r));
  438. header_.width = width;
  439. header_.height = height;
  440. WImage<T>::SetIpl(&header_);
  441. }
  442. template<typename T>
  443. WImageView<T>::WImageView(T* data, int width, int height, int nchannels, int width_step)
  444. : WImage<T>(0)
  445. {
  446. cvInitImageHeader(&header_, cvSize(width, height), WImage<T>::Depth(), nchannels);
  447. header_.imageData = reinterpret_cast<char*>(data);
  448. if (width_step > 0) {
  449. header_.widthStep = width_step;
  450. }
  451. WImage<T>::SetIpl(&header_);
  452. }
  453. template<typename T, int C>
  454. WImageViewC<T, C>::WImageViewC(WImageC<T, C>* img, int c, int r, int width, int height)
  455. : WImageC<T, C>(0)
  456. {
  457. header_ = *(img->Ipl());
  458. header_.imageData = reinterpret_cast<char*>((*img)(c, r));
  459. header_.width = width;
  460. header_.height = height;
  461. WImageC<T, C>::SetIpl(&header_);
  462. }
  463. template<typename T, int C>
  464. WImageViewC<T, C>::WImageViewC() : WImageC<T, C>(0) {
  465. cvInitImageHeader(&header_, cvSize(0, 0), WImage<T>::Depth(), C);
  466. header_.imageData = reinterpret_cast<char*>(0);
  467. WImageC<T, C>::SetIpl(&header_);
  468. }
  469. template<typename T, int C>
  470. WImageViewC<T, C>::WImageViewC(T* data, int width, int height, int width_step)
  471. : WImageC<T, C>(0)
  472. {
  473. cvInitImageHeader(&header_, cvSize(width, height), WImage<T>::Depth(), C);
  474. header_.imageData = reinterpret_cast<char*>(data);
  475. if (width_step > 0) {
  476. header_.widthStep = width_step;
  477. }
  478. WImageC<T, C>::SetIpl(&header_);
  479. }
  480. // Construct a view into a region of an image
  481. template<typename T>
  482. WImageView<T> WImage<T>::View(int c, int r, int width, int height) {
  483. return WImageView<T>(this, c, r, width, height);
  484. }
  485. template<typename T, int C>
  486. WImageViewC<T, C> WImageC<T, C>::View(int c, int r, int width, int height) {
  487. return WImageViewC<T, C>(this, c, r, width, height);
  488. }
  489. //! @} core
  490. } // end of namespace
  491. #endif // __cplusplus
  492. #endif