1 /*
2 #
3 # File : ipl_alt.h
4 # ( C++ header file - CImg plug-in )
5 #
6 # Description : CImg plug-in providing the CImg->IPL and IPL->CImg
7 # conversions for generic image types
8 # ( IPL = Intel Performance Library )
9 # This file is a part of the CImg Library project.
10 # ( http://cimg.eu )
11 #
12 # Copyright : newleft (haibo.zheng@gmail.com)
13 # newleftist@hotmail.com
14 #
15 # License : CeCILL v2.0
16 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
17 #
18 # This software is governed by the CeCILL license under French law and
19 # abiding by the rules of distribution of free software. You can use,
20 # modify and/ or redistribute the software under the terms of the CeCILL
21 # license as circulated by CEA, CNRS and INRIA at the following URL
22 # "http://www.cecill.info".
23 #
24 # As a counterpart to the access to the source code and rights to copy,
25 # modify and redistribute granted by the license, users are provided only
26 # with a limited warranty and the software's author, the holder of the
27 # economic rights, and the successive licensors have only limited
28 # liability.
29 #
30 # In this respect, the user's attention is drawn to the risks associated
31 # with loading, using, modifying and/or developing or reproducing the
32 # software by the user in light of its specific status of free software,
33 # that may mean that it is complicated to manipulate, and that also
34 # therefore means that it is reserved for developers and experienced
35 # professionals having in-depth computer knowledge. Users are therefore
36 # encouraged to load and test the software's suitability as regards their
37 # requirements in conditions enabling the security of their systems and/or
38 # data to be ensured and, more generally, to use and operate it in the
39 # same conditions as regards security.
40 #
41 # The fact that you are presently reading this means that you have had
42 # knowledge of the CeCILL license and that you accept its terms.
43 #
44 */
45
46 #ifndef cimg_plugin_IPL
47 #define cimg_plugin_IPL
48
49 // Conversion IPL -> CImg (constructor)
CImg(const IplImage * src)50 CImg(const IplImage* src):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
51 assign(src);
52 }
53
54 // Conversion IPL -> CImg (in-place constructor)
assign(const IplImage * src)55 CImg<T>& assign(const IplImage* src) {
56 if (!src) return assign();
57 switch (src->depth) {
58 case IPL_DEPTH_1U: { // 1-bit int.
59 IplImage *src1 = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
60 cvConvert(src,src1);
61 CImg<ucharT>((unsigned char*)src1->imageData,src1->nChannels,src1->width,src1->height,1,true).
62 get_permute_axes("yzcx").move_to(*this);
63 cvReleaseImage(&src1);
64 } break;
65 case IPL_DEPTH_8U: // 8-bit unsigned int.
66 CImg<ucharT>((unsigned char*)src->imageData,src->nChannels,src->width,src->height,1,true).
67 get_permute_axes("yzcx").move_to(*this);
68 break;
69 case IPL_DEPTH_8S: // 8-bit signed int.
70 CImg<charT>((char*)src->imageData,src->nChannels,src->width,src->height,1,true).
71 get_permute_axes("yzcx").move_to(*this);
72 break;
73 case IPL_DEPTH_16U: // 16-bit unsigned int.
74 CImg<ushortT>((unsigned short*)src->imageData,src->nChannels,src->width,src->height,1,true).
75 get_permute_axes("yzcx").move_to(*this);
76 break;
77 case IPL_DEPTH_16S: // 16-bit signed int.
78 CImg<shortT>((short*)src->imageData,src->nChannels,src->width,src->height,1,true).
79 get_permute_axes("yzcx").move_to(*this);
80 break;
81 case IPL_DEPTH_32S: // 32-bit signed int.
82 CImg<intT>((int*)src->imageData,src->nChannels,src->width,src->height,1,true).
83 get_permute_axes("yzcx").move_to(*this);
84 break;
85 case IPL_DEPTH_32F: // 32-bit float.
86 CImg<floatT>((float*)src->imageData,src->nChannels,src->width,src->height,1,true).
87 get_permute_axes("yzcx").move_to(*this);
88 break;
89 case IPL_DEPTH_64F: // 64-bit double.
90 CImg<doubleT>((double*)src->imageData,src->nChannels,src->width,src->height,1,true).
91 get_permute_axes("yzcx").move_to(*this);
92 break;
93 default:
94 throw CImgInstanceException("CImg<%s>::assign(const IplImage* img) : IplImage depth is invalid.",
95 pixel_type());
96 break;
97 }
98 if (!std::strcmp(src->channelSeq,"BGR")) mirror('v');
99 else if (!std::strcmp(src->channelSeq,"BGRA")) get_shared_channels(0,2).mirror('v');
100 return *this;
101 }
102
103 // Conversion CImg -> IPL
104 IplImage* get_IPL(const unsigned int z=0) const {
105 if (is_empty())
106 throw CImgInstanceException("CImg<%s>::get_IPL() : instance image (%u,%u,%u,%u,%p) is empty.",
107 pixel_type(),_width,_height,_depth,_spectrum,_data);
108 if (z>=_depth)
109 throw CImgInstanceException("CImg<%s>::get_IPL() : specified slice %u is out of image bounds (%u,%u,%u,%u,%p).",
110 pixel_type(),z,_width,_height,_depth,_spectrum,_data);
111 const CImg<T>
112 _slice = _depth>1?get_slice(z):CImg<T>(),
113 &slice = _depth>1?_slice:*this;
114 CImg<T> buf(slice);
115 if (_spectrum==3 || _spectrum==4) buf.get_shared_channels(0,2).mirror('v');
116 buf.permute_axes("cxyz");
117 IplImage* const dst = cvCreateImage(cvSize(_width,_height),sizeof(T)*8,_spectrum);
118 std::memcpy(dst->imageData,buf.data(),buf.size()*sizeof(T));
119 return dst;
120 }
121
122 #endif /* cimg_plugin_IPL */
123