00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef IPLV_BASICIMAGE_HXX
00025 #define IPLV_BASICIMAGE_HXX
00026
00027
00028 #include "vigra/multi_array.hxx"
00029 #include "vigra/imageiterator.hxx"
00030 #include "vigra/stdimage.hxx"
00031 #include "vigra/basicimageview.hxx"
00032 #include "vigra/basicimage.hxx"
00033
00034 #include "vimageoperation.hxx"
00035
00036 #include <boost/mpl/if.hpp>
00037
00038
00039
00040 #include <ipl.h>
00041 #include "cv.h"
00042
00043
00044
00045
00046
00047 template<class PIXELTYPE>class CiplImageDepth {public: int depth;CiplImageDepth(){depth= sizeof(PIXELTYPE);}};
00048 template<>class CiplImageDepth<unsigned char> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_8U;}};
00049 template<>class CiplImageDepth<char> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_8S;}};
00050 template<>class CiplImageDepth<unsigned short> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_16U;}};
00051 template<>class CiplImageDepth<short> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_16S;}};
00052 template<>class CiplImageDepth<int> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_32S;}};
00053 template<>class CiplImageDepth<float> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_32F;}};
00054 template<>class CiplImageDepth<double> {public: int depth;CiplImageDepth(){depth= IPL_DEPTH_64F;}};
00055
00056 template<class PIXELTYPE>class CipliConstImgDepth {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(sizeof(PIXELTYPE)){}};
00057 template<>class CipliConstImgDepth<unsigned char> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_8U){}};
00058 template<>class CipliConstImgDepth<char> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_8S){}};
00059 template<>class CipliConstImgDepth<unsigned short> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_16U){}};
00060 template<>class CipliConstImgDepth<short> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_16S){}};
00061 template<>class CipliConstImgDepth<int> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_32S){}};
00062 template<>class CipliConstImgDepth<float> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_32F){}};
00063 template<>class CipliConstImgDepth<double> {public: const int depthConstInt;CipliConstImgDepth():depthConstInt(IPL_DEPTH_64F){}};
00064
00065
00066
00067
00068
00069
00070
00071
00154 template <
00155 class PIXELTYPE
00156 ,class Alloc = std::allocator<PIXELTYPE>
00157 >
00158 class CiplvBasicImage : public CipliConstImgDepth<PIXELTYPE>,public vigra::BasicImageView<PIXELTYPE>
00159 {
00160 public:
00161 int iplDepthDef;
00162
00163 IplImage *pIplImage;
00164
00165 IplImage* initProc(int width,int height)
00166 {
00167 CiplImageDepth<PIXELTYPE> iplImageDepth;
00168 iplDepthDef=iplImageDepth.depth;
00169 pIplImage=
00170 cvCreateImage( cvSize(width, height),
00171 iplDepthDef
00172 , 1 );
00173
00174 return pIplImage;
00175 }
00176
00177 IplImage* initAndCopyProc(IplImage ipl_imageIn)
00178 {
00179 pIplImage=initProc(ipl_imageIn.widthStep/sizeof(PIXELTYPE),ipl_imageIn.height);
00180 pIplImage=iplCloneImage (&ipl_imageIn);
00181 return pIplImage;
00182 }
00183
00184 int iplWidth()
00185 {
00186 return (pIplImage->width);
00187 }
00188
00189
00190 IplImage* loadImageAndInit(char * filename)
00191 {
00192 IplImage* grayImage = cvLoadImage( filename, 0 );
00193 return grayImage;
00194 }
00195
00196 IplImage* loadImageAndInit(const char * filename)
00197 {
00198 IplImage* grayImage = cvLoadImage( filename, 0 );
00199 return grayImage;
00200 }
00201
00202 CiplvBasicImage( char *filename) :
00203 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00204 (pIplImage=loadImageAndInit(filename))->imageData), pIplImage->widthStep/sizeof(PIXELTYPE),pIplImage->height)
00205 {}
00206
00207 CiplvBasicImage(const char * filename ) :
00208 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00209 (pIplImage=loadImageAndInit(filename))->imageData), pIplImage->widthStep/sizeof(PIXELTYPE),pIplImage->height)
00210 {}
00211
00212
00213 CiplvBasicImage(int width,int height) :
00214 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00215 (pIplImage=initProc(width,height) )->imageData)
00216 , pIplImage->widthStep/sizeof(PIXELTYPE),height)
00217 {}
00218
00219 CiplvBasicImage(IplImage* pIplImageIn) :
00220 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00221 pIplImageIn->imageData)
00222 , pIplImageIn->widthStep/sizeof(PIXELTYPE),pIplImageIn->height)
00223 {
00224 pIplImage=pIplImageIn;
00225 }
00226
00227 CiplvBasicImage(IplImage ipl_imageIn) :
00228 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00229 (pIplImage=initAndCopyProc(ipl_imageIn))->imageData)
00230 ,ipl_imageIn.widthStep/sizeof(PIXELTYPE)
00231 ,ipl_imageIn.height)
00232 {}
00233
00234 CiplvBasicImage(CiplvBasicImage<PIXELTYPE> &iplvImageIn) :
00235 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00236 (pIplImage=initProc(iplvImageIn.width(),iplvImageIn.height() ) )->imageData)
00237 , pIplImage->widthStep/sizeof(PIXELTYPE),iplvImageIn.height()
00238 )
00239 {
00240 iplvResizeCopyImageZeroPad(&iplvImageIn , this);
00241 }
00242
00243 #if 0
00244 CiplvBasicImage(CvMat* im) :
00245 vigra::BasicImageView< PIXELTYPE >(reinterpret_cast<PIXELTYPE*>(
00246 im->data.ptr)
00247 , im->width,im->height)
00248 {
00249 pIplImage = new IplImage ;
00250
00251
00252 pIplImage->imageData=(char *)im->data.ptr;
00253 pIplImage->width=im->width;
00254 pIplImage->height=im->height;
00255 }
00256 #endif
00257
00258 ~CiplvBasicImage(){
00259 cvReleaseImage( &pIplImage);
00260
00261
00262 }
00263 };
00264
00265 typedef CiplvBasicImage<unsigned char> CiplvBImage;
00266
00267
00268 #if 1
00269
00270 template <class PIXELTYPE, class Alloc = std::allocator<PIXELTYPE> >
00271 class CviplBasicImage : public CipliConstImgDepth<PIXELTYPE>, public vigra::BasicImage<PIXELTYPE,Alloc>, public IplImage
00272 {
00273 public:
00274 int iplDepthDef;
00275 IplImage *const pIplImage;
00276
00277
00278
00279 IplImage*
00280 viplInitImageHeader( IplImage * image, CvSize size, int depth,
00281 int channels, int origin=0,int align=4 )
00282 {
00283 IplImage* result = 0;
00284
00285 char *colorModel, *channelSeq;
00286
00287 if( !image )
00288 CV_ERROR( CV_HeaderIsNull, "null pointer to header" );
00289
00290 memset( image, 0, sizeof( *image ));
00291 image->nSize = sizeof( *image );
00292
00293 CV_CALL( icvGetColorModel( channels, &colorModel, &channelSeq ));
00294 strncpy( image->colorModel, colorModel, 4 );
00295 strncpy( image->channelSeq, channelSeq, 4 );
00296
00297 if( size.width < 0 || size.height < 0 )
00298 CV_ERROR( CV_BadROISize, "Bad input roi" );
00299
00300 if( (depth != (int)IPL_DEPTH_1U && depth != (int)IPL_DEPTH_8U &&
00301 depth != (int)IPL_DEPTH_8S && depth != (int)IPL_DEPTH_16U &&
00302 depth != (int)IPL_DEPTH_16S && depth != (int)IPL_DEPTH_32S &&
00303 depth != (int)IPL_DEPTH_32F && depth != (int)IPL_DEPTH_64F) ||
00304 channels < 0 )
00305 CV_ERROR( CV_BadDepth, "Unsupported format" );
00306 if( origin != CV_ORIGIN_BL && origin != CV_ORIGIN_TL )
00307 CV_ERROR( CV_BadOrigin, "Bad input origin" );
00308
00309 if( align != 4 && align != 8 )
00310 CV_ERROR( CV_BadAlign, "Bad input align" );
00311
00312 image->width = size.width;
00313 image->height = size.height;
00314
00315 if( image->roi )
00316 {
00317 image->roi->coi = 0;
00318 image->roi->xOffset = image->roi->yOffset = 0;
00319 image->roi->width = size.width;
00320 image->roi->height = size.height;
00321 }
00322
00323 image->nChannels = MAX( channels, 1 );
00324 image->depth = depth;
00325 image->align = align;
00327 image->widthStep = image->width*sizeof(PIXELTYPE);
00329 image->origin = origin;
00330 image->imageSize = image->widthStep * image->height;
00331
00332 result = image;
00333 return result;
00334 }
00335 #if 0
00336 IplImage* initIplImage(int width,int height)
00337 {
00338 CiplImageDepth<PIXELTYPE> iplImageDepth;
00339 iplDepthDef=iplImageDepth.depth;
00340 viplInitImageHeader(
00341 (IplImage *)this,
00342 , cvSize(width,height), iplDepthDef,1);
00343 return (IplImage *)this;
00344 }
00345 #endif
00346
00347 CviplBasicImage(int width,int height) :
00348 CipliConstImgDepth<PIXELTYPE>(),
00349 vigra::BasicImage<PIXELTYPE,Alloc>(width,height),
00350 pIplImage(
00351 viplInitImageHeader(
00352 reinterpret_cast<IplImage*>this,
00353 cvSize(width,height),depthConstInt,1
00354 )
00355 )
00356 {}
00357
00358
00359 };
00360
00361 #endif
00362
00363
00364
00365
00366
00367
00368 #endif
00369