1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
43 // Image.cpp: implementation of the CvvImage class.
45 //////////////////////////////////////////////////////////////////////
49 #if defined __cplusplus && (!defined WIN32 || !defined __GNUC__)
51 //////////////////////////////////////////////////////////////////////
52 // Construction/Destruction
53 //////////////////////////////////////////////////////////////////////
60 void CvvImage::Destroy()
62 cvReleaseImage( &m_img );
70 bool CvvImage::Create( int w, int h, int bpp, int origin )
72 const unsigned max_img_size = 10000;
74 if( (bpp != 8 && bpp != 24 && bpp != 32) ||
75 (unsigned)w >= max_img_size || (unsigned)h >= max_img_size ||
76 (origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))
78 assert(0); // most probably, it is a programming error
82 if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h )
84 if( m_img && m_img->nSize == sizeof(IplImage))
87 /* prepare IPL header */
88 m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 );
92 m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;
97 void CvvImage::CopyOf( CvvImage& image, int desired_color )
99 IplImage* img = image.GetImage();
102 CopyOf( img, desired_color );
107 #define HG_IS_IMAGE(img) \
108 ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \
109 ((IplImage*)img)->imageData != 0)
112 void CvvImage::CopyOf( IplImage* img, int desired_color )
114 if( HG_IS_IMAGE(img) )
116 int color = desired_color;
117 CvSize size = cvGetSize( img );
120 color = img->nChannels > 1;
122 if( Create( size.width, size.height,
123 (!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8,
126 cvConvertImage( img, m_img, 0 );
132 bool CvvImage::Load( const char* filename, int desired_color )
134 IplImage* img = cvLoadImage( filename, desired_color );
138 CopyOf( img, desired_color );
139 cvReleaseImage( &img );
145 bool CvvImage::LoadRect( const char* filename,
146 int desired_color, CvRect r )
148 if( r.width < 0 || r.height < 0 ) return false;
150 IplImage* img = cvLoadImage( filename, desired_color );
154 if( r.width == 0 || r.height == 0 )
156 r.width = img->width;
157 r.height = img->height;
161 if( r.x > img->width || r.y > img->height ||
162 r.x + r.width < 0 || r.y + r.height < 0 )
164 cvReleaseImage( &img );
168 /* truncate r to source image */
180 if( r.x + r.width > img->width )
181 r.width = img->width - r.x;
183 if( r.y + r.height > img->height )
184 r.height = img->height - r.y;
186 cvSetImageROI( img, r );
187 CopyOf( img, desired_color );
189 cvReleaseImage( &img );
194 bool CvvImage::Save( const char* filename )
198 cvSaveImage( filename, m_img );
203 void CvvImage::Show( const char* window )
206 cvShowImage( window, m_img );
212 void CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )
214 if( m_img && m_img->depth == IPL_DEPTH_8U )
216 uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
217 BITMAPINFO* bmi = (BITMAPINFO*)buffer;
218 int bmp_w = m_img->width, bmp_h = m_img->height;
220 FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
222 from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );
223 from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );
225 int sw = MAX( MIN( bmp_w - from_x, w ), 0 );
226 int sh = MAX( MIN( bmp_h - from_y, h ), 0 );
229 dc, x, y, sw, sh, from_x, from_y, from_y, sh,
230 m_img->imageData + from_y*m_img->widthStep,
231 bmi, DIB_RGB_COLORS );
236 void CImage::DrawToHDC( HDC hDCDst, RECT* pDstRect )
238 if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )
240 uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
241 BITMAPINFO* bmi = (BITMAPINFO*)buffer;
242 int bmp_w = m_img->width, bmp_h = m_img->height;
244 CvRect roi = cvGetImageROI( m_img );
245 CvRect dst = RectToCvRect( *pDstRect );
247 if( roi.width == dst.width && roi.height == dst.height )
249 Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );
253 if( roi.width > dst.width )
256 hDCDst, // handle to device context
262 hDCDst, // handle to device context
266 FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
270 dst.x, dst.y, dst.width, dst.height,
271 roi.x, roi.y, roi.width, roi.height,
272 m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY );
278 void CvvImage::Fill( int color )
280 cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );