Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / otherlibs / highgui / cvcap_mil.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 #include "_highgui.h"
43 #include "mil.h" 
44
45 #if _MSC_VER >= 1200
46   #pragma warning( disable: 4711 )
47   #pragma comment(lib,"mil.lib")
48   #pragma comment(lib,"milmet2.lib")
49 #endif
50
51 #if defined WIN64 && defined EM64T && defined _MSC_VER && !defined __ICL
52   #pragma optimize("",off)
53 #endif
54
55 /********************* Capturing video from camera via MIL *********************/
56
57 struct 
58 {      
59     MIL_ID MilApplication;
60     int MilUser;
61 } g_Mil = {0,0}; //global structure for handling MIL application
62
63 class CvCaptureCAM_MIL : public CvCapture
64 {
65 public:
66     CvCaptureCAM_MIL() { init(); }
67     virtual ~CvCaptureCAM_MIL() { close(); }
68
69     virtual bool open( int index );
70     virtual void close();
71
72     virtual double getProperty(int);
73     virtual bool setProperty(int, double) { return false; }
74     virtual bool grabFrame();
75     virtual IplImage* retrieveFrame();
76
77 protected:
78     void init();
79
80     MIL_ID 
81         MilSystem,       /* System identifier.       */
82         MilDisplay,      /* Display identifier.      */
83         MilDigitizer,    /* Digitizer identifier.    */ 
84         MilImage;        /* Image buffer identifier. */
85     IplImage* rgb_frame;
86 };
87
88
89 void CvCaptureCAM_MIL::init()
90 {
91     MilSystem = MilDisplay = MilDigitizer = MilImage = M_NULL;
92     rgb_frame = 0;
93 }
94
95 // Initialize camera input
96 bool CvCaptureCAM_MIL::open( int wIndex )
97 {
98     close();
99
100     if( g_Mil.MilApplication == M_NULL )
101     {
102         assert(g_Mil.MilUser == 0);
103         MappAlloc(M_DEFAULT, &(g_Mil.MilApplication) );
104         g_Mil.MilUser = 1;
105     }
106     else
107     {
108         assert(g_Mil.MilUser>0);
109         g_Mil.MilUser++;
110     }
111     
112     int dev_table[16] = { M_DEV0, M_DEV1, M_DEV2, M_DEV3,
113         M_DEV4, M_DEV5, M_DEV6, M_DEV7,
114         M_DEV8, M_DEV9, M_DEV10, M_DEV11,
115         M_DEV12, M_DEV13, M_DEV14, M_DEV15 };
116
117     //set default window size
118     int w = 320;
119     int h = 240;
120     
121     for( ; wIndex < 16; wIndex++ ) 
122     {
123         MsysAlloc( M_SYSTEM_SETUP, //we use default system,
124                                    //if this does not work 
125                                    //try to define exact board 
126                                    //e.g.M_SYSTEM_METEOR,M_SYSTEM_METEOR_II...
127                    dev_table[wIndex], 
128                    M_DEFAULT, 
129                    &MilSystem ); 
130
131         if( MilSystem != M_NULL )
132             break;
133     }
134     if( MilSystem != M_NULL )
135     {
136         MdigAlloc(MilSystem,M_DEFAULT,
137                   M_CAMERA_SETUP, //default. May be M_NTSC or other
138                   M_DEFAULT,&MilDigitizer);
139         
140         rgb_frame = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 3 );
141         MdigControl(MilDigitizer, M_GRAB_SCALE,  1.0 / 2);
142         
143         /*below line enables getting image vertical orientation 
144          consistent with VFW but it introduces some image corruption 
145          on MeteorII, so we left the image as is*/  
146         //MdigControl(MilDigitizer, M_GRAB_DIRECTION_Y, M_REVERSE );
147
148         MilImage = MbufAllocColor(MilSystem, 3, w, h,
149             8+M_UNSIGNED, M_IMAGE + M_GRAB, M_NULL);
150     }
151     
152     return MilSystem != M_NULL;
153 }
154
155 void CvCaptureCAM_MIL::close( CvCaptureCAM_MIL* capture )
156 {
157     if( MilSystem != M_NULL )
158     {
159         MdigFree( MilDigitizer );
160         MbufFree( MilImage );
161         MsysFree( MilSystem );
162         cvReleaseImage(&rgb_frame ); 
163         
164         g_Mil.MilUser--;
165         if(!g_Mil.MilUser)
166             MappFree(g_Mil.MilApplication);
167
168         MilSystem = M_NULL;
169         MilDigitizer = M_NULL;
170     }
171 }         
172
173
174 bool CvCaptureCAM_MIL::grabFrame()
175 {
176     if( MilSystem )
177     {
178         MdigGrab(MilDigitizer, MilImage);
179         return true;
180     }
181     return false;
182 }
183
184
185 IplImage* CvCaptureCAM_MIL::retrieveFrame()
186 {
187     MbufGetColor(MilImage, M_BGR24+M_PACKED, M_ALL_BAND, (void*)(rgb_frame->imageData)); 
188     return rgb_frame;
189 }
190
191 double CvCaptureCAM_MIL::getProperty( int property_id )
192 {
193     switch( property_id )
194     {
195         case CV_CAP_PROP_FRAME_WIDTH:
196         return rgb_frame ? rgb_frame->width : 0;
197         case CV_CAP_PROP_FRAME_HEIGHT:
198                 return rgb_frame ? rgb_frame->height : 0;
199     } 
200     return 0;
201 }
202
203 bool CvCaptureCAM_MIL::setProperty( int, double )
204 {
205     return false;
206 }
207
208
209 CvCapture* cvCreateCameraCapture_MIL( int index )
210 {
211         CvCaptureCAM_MIL* capture = new CvCaptureCAM_MIL;
212
213     if( capture->open( index ))
214         return capture;
215
216     delete capture;
217     return 0;
218 }