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