PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/external/opencv/cxcore/src/cximage.cpp

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C++ | 397 lines | 245 code | 72 blank | 80 comment | 52 complexity | 3d214b99ad4e6ade3078779c543a921b MD5 | raw file
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or 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. /* ////////////////////////////////////////////////////////////////////
  42. //
  43. // C++ classes for image and matrices
  44. //
  45. // */
  46. #include "_cxcore.h"
  47. /////////////////////////////// CvImage implementation //////////////////////////////////
  48. static CvLoadImageFunc load_image = 0;
  49. static CvLoadImageMFunc load_image_m = 0;
  50. static CvSaveImageFunc save_image = 0;
  51. static CvShowImageFunc show_image = NULL;
  52. static bool
  53. icvIsXmlOrYaml( const char* filename )
  54. {
  55. const char* suffix = strrchr( filename, '.' );
  56. return suffix &&
  57. (strcmp( suffix, ".xml" ) == 0 ||
  58. strcmp( suffix, ".Xml" ) == 0 ||
  59. strcmp( suffix, ".XML" ) == 0 ||
  60. strcmp( suffix, ".yml" ) == 0 ||
  61. strcmp( suffix, ".Yml" ) == 0 ||
  62. strcmp( suffix, ".YML" ) == 0 ||
  63. strcmp( suffix, ".yaml" ) == 0 ||
  64. strcmp( suffix, ".Yaml" ) == 0 ||
  65. strcmp( suffix, ".YAML" ) == 0);
  66. }
  67. static IplImage*
  68. icvRetrieveImage( void* obj )
  69. {
  70. IplImage* img = 0;
  71. CV_FUNCNAME( "icvRetrieveImage" );
  72. __BEGIN__;
  73. if( CV_IS_IMAGE(obj) )
  74. img = (IplImage*)obj;
  75. else if( CV_IS_MAT(obj) )
  76. {
  77. CvMat* m = (CvMat*)obj;
  78. CV_CALL( img = cvCreateImageHeader( cvSize(m->cols,m->rows),
  79. CV_MAT_DEPTH(m->type), CV_MAT_CN(m->type) ));
  80. cvSetData( img, m->data.ptr, m->step );
  81. img->imageDataOrigin = (char*)m->refcount;
  82. m->data.ptr = 0; m->step = 0;
  83. cvReleaseMat( &m );
  84. }
  85. else if( obj )
  86. {
  87. cvRelease( &obj );
  88. CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
  89. }
  90. __END__;
  91. return img;
  92. }
  93. bool CvImage::load( const char* filename, const char* imgname, int color )
  94. {
  95. IplImage* img = 0;
  96. CV_FUNCNAME( "CvImage::read" );
  97. __BEGIN__;
  98. if( icvIsXmlOrYaml(filename) )
  99. {
  100. img = icvRetrieveImage(cvLoad(filename,0,imgname));
  101. if( (img->nChannels > 1) != (color == 0) )
  102. CV_ERROR( CV_StsNotImplemented,
  103. "RGB<->Grayscale conversion is not implemented for images stored in XML/YAML" );
  104. /*{
  105. IplImage* temp_img = 0;
  106. CV_CALL( temp_img = cvCreateImage( cvGetSize(img), img->depth, color > 0 ? 3 : 1 ));
  107. cvCvtColor( img, temp_img, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
  108. cvReleaseImage( &img );
  109. img = temp_img;
  110. }*/
  111. }
  112. else
  113. {
  114. if( load_image )
  115. img = load_image( filename, color );
  116. else
  117. CV_ERROR( CV_StsNotImplemented,
  118. "Loading an image stored in such a format requires HigGUI.\n"
  119. "Link it to your program and call any function from it\n" );
  120. }
  121. attach( img );
  122. __END__;
  123. return img != 0;
  124. }
  125. bool CvImage::read( CvFileStorage* fs, const char* mapname, const char* imgname )
  126. {
  127. void* obj = 0;
  128. IplImage* img = 0;
  129. if( mapname )
  130. {
  131. CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
  132. if( !mapnode )
  133. obj = cvReadByName( fs, mapnode, imgname );
  134. }
  135. else
  136. obj = cvReadByName( fs, 0, imgname );
  137. img = icvRetrieveImage(obj);
  138. attach( img );
  139. return img != 0;
  140. }
  141. bool CvImage::read( CvFileStorage* fs, const char* seqname, int idx )
  142. {
  143. void* obj = 0;
  144. IplImage* img = 0;
  145. CvFileNode* seqnode = seqname ?
  146. cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
  147. if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
  148. obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
  149. img = icvRetrieveImage(obj);
  150. attach( img );
  151. return img != 0;
  152. }
  153. void CvImage::save( const char* filename, const char* imgname )
  154. {
  155. CV_FUNCNAME( "CvImage::write" );
  156. __BEGIN__;
  157. if( !image )
  158. return;
  159. if( icvIsXmlOrYaml( filename ) )
  160. cvSave( filename, image, imgname );
  161. else
  162. {
  163. if( save_image )
  164. save_image( filename, image );
  165. else
  166. CV_ERROR( CV_StsNotImplemented,
  167. "Saving an image in such a format requires HigGUI.\n"
  168. "Link it to your program and call any function from it\n" );
  169. }
  170. __END__;
  171. }
  172. void CvImage::write( CvFileStorage* fs, const char* imgname )
  173. {
  174. if( image )
  175. cvWrite( fs, imgname, image );
  176. }
  177. /////////////////////////////// CvMatrix implementation //////////////////////////////////
  178. CvMatrix::CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data )
  179. {
  180. if( storage )
  181. {
  182. matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
  183. cvInitMatHeader( matrix, rows, cols, type, alloc_data ?
  184. cvMemStorageAlloc( storage, rows*cols*CV_ELEM_SIZE(type) ) : 0 );
  185. }
  186. else
  187. matrix = 0;
  188. }
  189. static CvMat*
  190. icvRetrieveMatrix( void* obj )
  191. {
  192. CvMat* m = 0;
  193. CV_FUNCNAME( "icvRetrieveMatrix" );
  194. __BEGIN__;
  195. if( CV_IS_MAT(obj) )
  196. m = (CvMat*)obj;
  197. else if( CV_IS_IMAGE(obj) )
  198. {
  199. IplImage* img = (IplImage*)obj;
  200. CvMat hdr, *src = cvGetMat( img, &hdr );
  201. CV_CALL( m = cvCreateMat( src->rows, src->cols, src->type ));
  202. CV_CALL( cvCopy( src, m ));
  203. cvReleaseImage( &img );
  204. }
  205. else if( obj )
  206. {
  207. cvRelease( &obj );
  208. CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
  209. }
  210. __END__;
  211. return m;
  212. }
  213. bool CvMatrix::load( const char* filename, const char* matname, int color )
  214. {
  215. CvMat* m = 0;
  216. CV_FUNCNAME( "CvMatrix::read" );
  217. __BEGIN__;
  218. if( icvIsXmlOrYaml(filename) )
  219. {
  220. m = icvRetrieveMatrix(cvLoad(filename,0,matname));
  221. if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
  222. CV_ERROR( CV_StsNotImplemented,
  223. "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
  224. /*{
  225. CvMat* temp_mat;
  226. CV_CALL( temp_mat = cvCreateMat( m->rows, m->cols,
  227. CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
  228. cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
  229. cvReleaseMat( &m );
  230. m = temp_mat;
  231. }*/
  232. }
  233. else
  234. {
  235. if( load_image_m )
  236. m = load_image_m( filename, color );
  237. else
  238. CV_ERROR( CV_StsNotImplemented,
  239. "Loading an image stored in such a format requires HigGUI.\n"
  240. "Link it to your program and call any function from it\n" );
  241. }
  242. set( m, false );
  243. __END__;
  244. return m != 0;
  245. }
  246. bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
  247. {
  248. void* obj = 0;
  249. CvMat* m = 0;
  250. if( mapname )
  251. {
  252. CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
  253. if( !mapnode )
  254. obj = cvReadByName( fs, mapnode, matname );
  255. }
  256. else
  257. obj = cvReadByName( fs, 0, matname );
  258. m = icvRetrieveMatrix(obj);
  259. set( m, false );
  260. return m != 0;
  261. }
  262. bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
  263. {
  264. void* obj = 0;
  265. CvMat* m = 0;
  266. CvFileNode* seqnode = seqname ?
  267. cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
  268. if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
  269. obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
  270. m = icvRetrieveMatrix(obj);
  271. set( m, false );
  272. return m != 0;
  273. }
  274. void CvMatrix::save( const char* filename, const char* matname )
  275. {
  276. CV_FUNCNAME( "CvMatrix::write" );
  277. __BEGIN__;
  278. if( !matrix )
  279. return;
  280. if( icvIsXmlOrYaml( filename ) )
  281. cvSave( filename, matrix, matname );
  282. else
  283. {
  284. if( save_image )
  285. save_image( filename, matrix );
  286. else
  287. CV_ERROR( CV_StsNotImplemented,
  288. "Saving a matrixe in such a format requires HigGUI.\n"
  289. "Link it to your program and call any function from it\n" );
  290. }
  291. __END__;
  292. }
  293. void CvMatrix::write( CvFileStorage* fs, const char* matname )
  294. {
  295. if( matrix )
  296. cvWrite( fs, matname, matrix );
  297. }
  298. CV_IMPL int
  299. cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,
  300. CvSaveImageFunc _save_image, CvShowImageFunc _show_image=NULL )
  301. {
  302. load_image = _load_image;
  303. load_image_m = _load_image_m;
  304. save_image = _save_image;
  305. show_image = _show_image;
  306. return 1;
  307. }
  308. /*void main(void)
  309. {
  310. CvImage a(cvSize(300,200),8,3), b(cvSize(300,200),8,3);
  311. CvRNG rng = cvRNG(-1);
  312. CV_SET_IMAGE_IO_FUNCTIONS();
  313. cvNamedWindow( "test", 1 );
  314. //cvZero( a );
  315. cvZero( b );
  316. cvRandArr( &rng, a, CV_RAND_UNI, cvScalarAll(0), cvScalarAll(100) );
  317. cvCircle( b, cvPoint(100,100), 70, cvScalar(0,255,0), -1, CV_AA, 0 );
  318. cvAdd( a, b, a );
  319. a.show( "test" );
  320. cvWaitKey();
  321. }*/
  322. /* End of file. */