PageRenderTime 39ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/external/opencv/otherlibs/highgui/image.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 255 lines | 156 code | 49 blank | 50 comment | 60 complexity | 66f7bdb838d6981bbd72c3970fb83b3f 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. // Image.cpp: implementation of the CvvImage class.
  43. //
  44. //////////////////////////////////////////////////////////////////////
  45. #include "_highgui.h"
  46. #if defined __cplusplus && (!defined WIN32 || !defined __GNUC__)
  47. //////////////////////////////////////////////////////////////////////
  48. // Construction/Destruction
  49. //////////////////////////////////////////////////////////////////////
  50. CvvImage::CvvImage()
  51. {
  52. m_img = 0;
  53. }
  54. void CvvImage::Destroy()
  55. {
  56. cvReleaseImage( &m_img );
  57. }
  58. CvvImage::~CvvImage()
  59. {
  60. Destroy();
  61. }
  62. bool CvvImage::Create( int w, int h, int bpp, int origin )
  63. {
  64. const unsigned max_img_size = 10000;
  65. if( (bpp != 8 && bpp != 24 && bpp != 32) ||
  66. (unsigned)w >= max_img_size || (unsigned)h >= max_img_size ||
  67. (origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))
  68. {
  69. assert(0); // most probably, it is a programming error
  70. return false;
  71. }
  72. if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h )
  73. {
  74. if( m_img && m_img->nSize == sizeof(IplImage))
  75. Destroy();
  76. /* prepare IPL header */
  77. m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 );
  78. }
  79. if( m_img )
  80. m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;
  81. return m_img != 0;
  82. }
  83. void CvvImage::CopyOf( CvvImage& image, int desired_color )
  84. {
  85. IplImage* img = image.GetImage();
  86. if( img )
  87. {
  88. CopyOf( img, desired_color );
  89. }
  90. }
  91. #define HG_IS_IMAGE(img) \
  92. ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \
  93. ((IplImage*)img)->imageData != 0)
  94. void CvvImage::CopyOf( IplImage* img, int desired_color )
  95. {
  96. if( HG_IS_IMAGE(img) )
  97. {
  98. int color = desired_color;
  99. CvSize size = cvGetSize( img );
  100. if( color < 0 )
  101. color = img->nChannels > 1;
  102. if( Create( size.width, size.height,
  103. (!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8,
  104. img->origin ))
  105. {
  106. cvConvertImage( img, m_img, 0 );
  107. }
  108. }
  109. }
  110. bool CvvImage::Load( const char* filename, int desired_color )
  111. {
  112. IplImage* img = cvLoadImage( filename, desired_color );
  113. if( !img )
  114. return false;
  115. CopyOf( img, desired_color );
  116. cvReleaseImage( &img );
  117. return true;
  118. }
  119. bool CvvImage::LoadRect( const char* filename,
  120. int desired_color, CvRect r )
  121. {
  122. if( r.width < 0 || r.height < 0 ) return false;
  123. IplImage* img = cvLoadImage( filename, desired_color );
  124. if( !img )
  125. return false;
  126. if( r.width == 0 || r.height == 0 )
  127. {
  128. r.width = img->width;
  129. r.height = img->height;
  130. r.x = r.y = 0;
  131. }
  132. if( r.x > img->width || r.y > img->height ||
  133. r.x + r.width < 0 || r.y + r.height < 0 )
  134. {
  135. cvReleaseImage( &img );
  136. return false;
  137. }
  138. /* truncate r to source image */
  139. if( r.x < 0 )
  140. {
  141. r.width += r.x;
  142. r.x = 0;
  143. }
  144. if( r.y < 0 )
  145. {
  146. r.height += r.y;
  147. r.y = 0;
  148. }
  149. if( r.x + r.width > img->width )
  150. r.width = img->width - r.x;
  151. if( r.y + r.height > img->height )
  152. r.height = img->height - r.y;
  153. cvSetImageROI( img, r );
  154. CopyOf( img, desired_color );
  155. cvReleaseImage( &img );
  156. return true;
  157. }
  158. bool CvvImage::Save( const char* filename )
  159. {
  160. if( !m_img )
  161. return false;
  162. cvSaveImage( filename, m_img );
  163. return true;
  164. }
  165. #ifdef WIN32
  166. void CImage::DrawToHDC( HDC hDCDst, RECT* pDstRect )
  167. {
  168. if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )
  169. {
  170. uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
  171. BITMAPINFO* bmi = (BITMAPINFO*)buffer;
  172. int bmp_w = m_img->width, bmp_h = m_img->height;
  173. CvRect roi = cvGetImageROI( m_img );
  174. CvRect dst = RectToCvRect( *pDstRect );
  175. if( roi.width == dst.width && roi.height == dst.height )
  176. {
  177. Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );
  178. return;
  179. }
  180. if( roi.width > dst.width )
  181. {
  182. SetStretchBltMode(
  183. hDCDst, // handle to device context
  184. HALFTONE );
  185. }
  186. else
  187. {
  188. SetStretchBltMode(
  189. hDCDst, // handle to device context
  190. COLORONCOLOR );
  191. }
  192. FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
  193. ::StretchDIBits(
  194. hDCDst,
  195. dst.x, dst.y, dst.width, dst.height,
  196. roi.x, roi.y, roi.width, roi.height,
  197. m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY );
  198. }
  199. }
  200. #endif
  201. void CvvImage::Fill( int color )
  202. {
  203. cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );
  204. }
  205. #endif
  206. /* End of file. */