Move the sources to trunk
[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
56
57
58 /********************* Capturing video from camera via MIL *********************/
59
60 struct 
61 {      
62     MIL_ID MilApplication;
63     int MilUser;
64 } g_Mil = {0,0}; //global structure for handling MIL application
65
66 typedef struct CvCaptureCAM_MIL
67 {
68     CvCaptureVTable* vtable;
69     MIL_ID 
70                 MilSystem,       /* System identifier.       */
71                 MilDisplay,      /* Display identifier.      */
72                 MilDigitizer,    /* Digitizer identifier.    */ 
73                 MilImage;        /* Image buffer identifier. */
74     IplImage* rgb_frame;
75 }
76 CvCaptureCAM_MIL;
77
78 // Initialize camera input
79 static int icvOpenCAM_MIL( CvCaptureCAM_MIL* capture, int wIndex )
80 {
81     if( g_Mil.MilApplication == M_NULL )
82     {
83         assert(g_Mil.MilUser == 0);
84         MappAlloc(M_DEFAULT, &(g_Mil.MilApplication) );
85         g_Mil.MilUser = 1;
86     }
87     else
88     {
89         assert(g_Mil.MilUser>0);
90         g_Mil.MilUser++;
91     }
92     
93     int dev_table[16] = { M_DEV0, M_DEV1, M_DEV2, M_DEV3,
94                 M_DEV4, M_DEV5, M_DEV6, M_DEV7,
95                 M_DEV8, M_DEV9, M_DEV10, M_DEV11,
96                 M_DEV12, M_DEV13, M_DEV14, M_DEV15 };
97     
98     //set default window size
99     int w = 320/*160*/;
100     int h = 240/*120*/;
101     
102     
103     for( ; wIndex < 16; wIndex++ ) 
104     {
105         MsysAlloc( M_SYSTEM_SETUP, //we use default system,
106                                    //if this does not work 
107                                    //try to define exact board 
108                                    //e.g.M_SYSTEM_METEOR,M_SYSTEM_METEOR_II...
109                    dev_table[wIndex], 
110                    M_DEFAULT, 
111                    &(capture->MilSystem) ); 
112                 
113         if( capture->MilSystem != M_NULL )
114             break;
115     }
116     if( capture->MilSystem != M_NULL )
117     {
118         MdigAlloc(capture->MilSystem,M_DEFAULT,
119                   M_CAMERA_SETUP, //default. May be M_NTSC or other
120                   M_DEFAULT,&(capture->MilDigitizer));
121         
122         capture->rgb_frame = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 3 );
123         MdigControl(capture->MilDigitizer, M_GRAB_SCALE,  1.0 / 2);
124         
125         /*below line enables getting image vertical orientation 
126                         consistent with VFW but it introduces some image corruption 
127                         on MeteorII, so we left the image as is*/  
128         //MdigControl(capture->MilDigitizer, M_GRAB_DIRECTION_Y, M_REVERSE );
129                 
130         capture->MilImage = MbufAllocColor(capture->MilSystem, 3, w, h,
131                                                                                    8+M_UNSIGNED,
132                                                                                    M_IMAGE + M_GRAB,                                                      
133                                                                                    M_NULL);
134     }
135     
136     return capture->MilSystem != M_NULL;
137 }
138
139 static  void icvCloseCAM_MIL( CvCaptureCAM_MIL* capture )
140 {
141     if( capture->MilSystem != M_NULL )
142     {
143         MdigFree( capture->MilDigitizer );
144         MbufFree( capture->MilImage );
145         MsysFree( capture->MilSystem );
146         cvReleaseImage(&capture->rgb_frame ); 
147         capture->rgb_frame = 0;
148         
149         g_Mil.MilUser--;
150         if(!g_Mil.MilUser)
151             MappFree(g_Mil.MilApplication);
152                 
153         capture->MilSystem = M_NULL;
154         capture->MilDigitizer = M_NULL;
155     }
156 }         
157
158
159 static int icvGrabFrameCAM_MIL( CvCaptureCAM_MIL* capture )
160 {
161     if( capture->MilSystem )
162     {
163         MdigGrab(capture->MilDigitizer, capture->MilImage);
164         return 1;
165     }
166     return 0;
167 }
168
169
170 static IplImage* icvRetrieveFrameCAM_MIL( CvCaptureCAM_MIL* capture )
171 {
172     MbufGetColor(capture->MilImage, M_BGR24+M_PACKED, M_ALL_BAND, (void*)(capture->rgb_frame->imageData)); 
173     //make image vertical orientation consistent with VFW
174     //You can find some better way to do this
175     capture->rgb_frame->origin = IPL_ORIGIN_BL;
176     cvFlip(capture->rgb_frame,capture->rgb_frame,0);
177     return capture->rgb_frame;
178 }
179
180 static double icvGetPropertyCAM_MIL( CvCaptureCAM_MIL* capture, int property_id )
181 {
182     switch( property_id )
183     {
184                 case CV_CAP_PROP_FRAME_WIDTH:
185                         if( capture->rgb_frame) return capture->rgb_frame->width;
186                 case CV_CAP_PROP_FRAME_HEIGHT:
187                         if( capture->rgb_frame) return capture->rgb_frame->height;
188     } 
189     return 0;
190 }
191
192 static CvCaptureVTable captureCAM_MIL_vtable = 
193 {
194     6,
195     (CvCaptureCloseFunc)icvCloseCAM_MIL,
196     (CvCaptureGrabFrameFunc)icvGrabFrameCAM_MIL,
197     (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_MIL,
198     (CvCaptureGetPropertyFunc)icvGetPropertyCAM_MIL,
199     (CvCaptureSetPropertyFunc)0,
200     (CvCaptureGetDescriptionFunc)0
201 };
202
203 CvCapture* cvCaptureFromCAM_MIL( int index )
204 {
205         CvCaptureCAM_MIL* capture = (CvCaptureCAM_MIL*)cvAlloc( sizeof(*capture));
206         memset( capture, 0, sizeof(*capture));
207         capture->vtable = &captureCAM_MIL_vtable;
208         
209         if( icvOpenCAM_MIL( capture, index ))
210                 return (CvCapture*)capture;
211         
212         cvReleaseCapture( (CvCapture**)&capture );
213         return 0;
214 }
215
216
217