PTdecode/CImg-1.3.0/plugins/cimgIPL.h

Mon, 03 Aug 2009 14:09:20 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 14:09:20 +0100
changeset 5
1204ebf9340d
permissions
-rwxr-xr-x

added P-touch decoder source

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