Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / otherlibs / highgui / image.cpp
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
55 CvvImage::CvvImage()
56 {
57     m_img = 0;
58 }
59
60 void CvvImage::Destroy()
61 {
62     cvReleaseImage( &m_img );
63 }
64
65 CvvImage::~CvvImage()
66 {
67     Destroy();
68 }
69
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
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
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
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
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
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 void  CvvImage::Show( const char* window )
204 {
205     if( m_img )
206         cvShowImage( window, m_img );
207 }
208
209
210 #ifdef WIN32
211
212 void  CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )
213 {
214     if( m_img && m_img->depth == IPL_DEPTH_8U )
215     {
216         uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
217         BITMAPINFO* bmi = (BITMAPINFO*)buffer;
218         int bmp_w = m_img->width, bmp_h = m_img->height;
219
220         FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
221
222         from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );
223         from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );
224
225         int sw = MAX( MIN( bmp_w - from_x, w ), 0 );
226         int sh = MAX( MIN( bmp_h - from_y, h ), 0 );
227
228         SetDIBitsToDevice(
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 );
232     }
233 }
234
235
236 void  CImage::DrawToHDC( HDC hDCDst, RECT* pDstRect ) 
237 {
238     if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )
239     {
240         uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
241         BITMAPINFO* bmi = (BITMAPINFO*)buffer;
242         int bmp_w = m_img->width, bmp_h = m_img->height;
243
244         CvRect roi = cvGetImageROI( m_img );
245         CvRect dst = RectToCvRect( *pDstRect );
246
247         if( roi.width == dst.width && roi.height == dst.height )
248         {
249             Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );
250             return;
251         }
252     
253         if( roi.width > dst.width )
254         {
255             SetStretchBltMode(
256                    hDCDst,           // handle to device context
257                    HALFTONE );
258         }
259         else
260         {
261             SetStretchBltMode(
262                    hDCDst,           // handle to device context
263                    COLORONCOLOR );
264         }
265
266         FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );
267
268         ::StretchDIBits(
269             hDCDst,
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 );
273     }
274 }
275
276 #endif
277
278 void  CvvImage::Fill( int color )
279 {
280     cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );
281 }
282
283 #endif
284
285 /* End of file. */