PageRenderTime 75ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/legacy/src/image.cpp

https://bitbucket.org/bosp/external-opencv
C++ | 310 lines | 196 code | 51 blank | 63 comment | 50 complexity | c6b88281a3f9a7a040bc9e23eb5c8f2b MD5 | raw file
Possible License(s): BSD-3-Clause
  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 "precomp.hpp"
  47. #include "opencv2/highgui/highgui_c.h"
  48. /////////////////////////////// CvImage implementation //////////////////////////////////
  49. static bool
  50. icvIsXmlOrYaml( const char* filename )
  51. {
  52. const char* suffix = strrchr( filename, '.' );
  53. return suffix &&
  54. (strcmp( suffix, ".xml" ) == 0 ||
  55. strcmp( suffix, ".Xml" ) == 0 ||
  56. strcmp( suffix, ".XML" ) == 0 ||
  57. strcmp( suffix, ".yml" ) == 0 ||
  58. strcmp( suffix, ".Yml" ) == 0 ||
  59. strcmp( suffix, ".YML" ) == 0 ||
  60. strcmp( suffix, ".yaml" ) == 0 ||
  61. strcmp( suffix, ".Yaml" ) == 0 ||
  62. strcmp( suffix, ".YAML" ) == 0);
  63. }
  64. static IplImage*
  65. icvRetrieveImage( void* obj )
  66. {
  67. IplImage* img = 0;
  68. if( CV_IS_IMAGE(obj) )
  69. img = (IplImage*)obj;
  70. else if( CV_IS_MAT(obj) )
  71. {
  72. CvMat* m = (CvMat*)obj;
  73. img = cvCreateImageHeader( cvSize(m->cols,m->rows),
  74. CV_MAT_DEPTH(m->type), CV_MAT_CN(m->type) );
  75. cvSetData( img, m->data.ptr, m->step );
  76. img->imageDataOrigin = (char*)m->refcount;
  77. m->data.ptr = 0; m->step = 0;
  78. cvReleaseMat( &m );
  79. }
  80. else if( obj )
  81. {
  82. cvRelease( &obj );
  83. CV_Error( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
  84. }
  85. return img;
  86. }
  87. bool CvImage::load( const char* filename, const char* imgname, int color )
  88. {
  89. IplImage* img = 0;
  90. if( icvIsXmlOrYaml(filename) )
  91. {
  92. img = icvRetrieveImage(cvLoad(filename,0,imgname));
  93. if( (img->nChannels > 1) != (color == 0) )
  94. CV_Error( CV_StsNotImplemented,
  95. "RGB<->Grayscale conversion is not implemented for images stored in XML/YAML" );
  96. /*{
  97. IplImage* temp_img = 0;
  98. temp_img = cvCreateImage( cvGetSize(img), img->depth, color > 0 ? 3 : 1 ));
  99. cvCvtColor( img, temp_img, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
  100. cvReleaseImage( &img );
  101. img = temp_img;
  102. }*/
  103. }
  104. else
  105. img = cvLoadImage( filename, color );
  106. attach( img );
  107. return img != 0;
  108. }
  109. bool CvImage::read( CvFileStorage* fs, const char* mapname, const char* imgname )
  110. {
  111. void* obj = 0;
  112. IplImage* img = 0;
  113. if( mapname )
  114. {
  115. CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
  116. if( !mapnode )
  117. obj = cvReadByName( fs, mapnode, imgname );
  118. }
  119. else
  120. obj = cvReadByName( fs, 0, imgname );
  121. img = icvRetrieveImage(obj);
  122. attach( img );
  123. return img != 0;
  124. }
  125. bool CvImage::read( CvFileStorage* fs, const char* seqname, int idx )
  126. {
  127. void* obj = 0;
  128. IplImage* img = 0;
  129. CvFileNode* seqnode = seqname ?
  130. cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
  131. if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
  132. obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
  133. img = icvRetrieveImage(obj);
  134. attach( img );
  135. return img != 0;
  136. }
  137. void CvImage::save( const char* filename, const char* imgname, const int* params )
  138. {
  139. if( !image )
  140. return;
  141. if( icvIsXmlOrYaml( filename ) )
  142. cvSave( filename, image, imgname );
  143. else
  144. cvSaveImage( filename, image, params );
  145. }
  146. void CvImage::write( CvFileStorage* fs, const char* imgname )
  147. {
  148. if( image )
  149. cvWrite( fs, imgname, image );
  150. }
  151. void CvImage::show( const char* window_name )
  152. {
  153. if( image )
  154. cvShowImage( window_name, image );
  155. }
  156. /////////////////////////////// CvMatrix implementation //////////////////////////////////
  157. CvMatrix::CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data )
  158. {
  159. if( storage )
  160. {
  161. matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
  162. cvInitMatHeader( matrix, rows, cols, type, alloc_data ?
  163. cvMemStorageAlloc( storage, rows*cols*CV_ELEM_SIZE(type) ) : 0 );
  164. }
  165. else
  166. matrix = 0;
  167. }
  168. static CvMat*
  169. icvRetrieveMatrix( void* obj )
  170. {
  171. CvMat* m = 0;
  172. if( CV_IS_MAT(obj) )
  173. m = (CvMat*)obj;
  174. else if( CV_IS_IMAGE(obj) )
  175. {
  176. IplImage* img = (IplImage*)obj;
  177. CvMat hdr, *src = cvGetMat( img, &hdr );
  178. m = cvCreateMat( src->rows, src->cols, src->type );
  179. cvCopy( src, m );
  180. cvReleaseImage( &img );
  181. }
  182. else if( obj )
  183. {
  184. cvRelease( &obj );
  185. CV_Error( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
  186. }
  187. return m;
  188. }
  189. bool CvMatrix::load( const char* filename, const char* matname, int color )
  190. {
  191. CvMat* m = 0;
  192. if( icvIsXmlOrYaml(filename) )
  193. {
  194. m = icvRetrieveMatrix(cvLoad(filename,0,matname));
  195. if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
  196. CV_Error( CV_StsNotImplemented,
  197. "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
  198. /*{
  199. CvMat* temp_mat;
  200. temp_mat = cvCreateMat( m->rows, m->cols,
  201. CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
  202. cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
  203. cvReleaseMat( &m );
  204. m = temp_mat;
  205. }*/
  206. }
  207. else
  208. m = cvLoadImageM( filename, color );
  209. set( m, false );
  210. return m != 0;
  211. }
  212. bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
  213. {
  214. void* obj = 0;
  215. CvMat* m = 0;
  216. if( mapname )
  217. {
  218. CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
  219. if( !mapnode )
  220. obj = cvReadByName( fs, mapnode, matname );
  221. }
  222. else
  223. obj = cvReadByName( fs, 0, matname );
  224. m = icvRetrieveMatrix(obj);
  225. set( m, false );
  226. return m != 0;
  227. }
  228. bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
  229. {
  230. void* obj = 0;
  231. CvMat* m = 0;
  232. CvFileNode* seqnode = seqname ?
  233. cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
  234. if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
  235. obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
  236. m = icvRetrieveMatrix(obj);
  237. set( m, false );
  238. return m != 0;
  239. }
  240. void CvMatrix::save( const char* filename, const char* matname, const int* params )
  241. {
  242. if( !matrix )
  243. return;
  244. if( icvIsXmlOrYaml( filename ) )
  245. cvSave( filename, matrix, matname );
  246. else
  247. cvSaveImage( filename, matrix, params );
  248. }
  249. void CvMatrix::write( CvFileStorage* fs, const char* matname )
  250. {
  251. if( matrix )
  252. cvWrite( fs, matname, matrix );
  253. }
  254. void CvMatrix::show( const char* window_name )
  255. {
  256. if( matrix )
  257. cvShowImage( window_name, matrix );
  258. }
  259. /* End of file. */