snapdvc.cpp

00001 
00003 #include "stdafx.h"
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <time.h>
00007 #include <windows.h>
00008 #include <math.h>
00009 
00010 #include <dvcAPI.h>
00011 
00012 
00013 
00014 int main(int argc, char* argv[])
00015 {       
00016         HANDLE hDevice ;
00017         int i, nStatus, nWidth, nHeight ;
00018         PUSHORT pRawVideoData ;
00019         PUSHORT pRGBVideoData ;
00020         BOOL bRC ;
00021         BOOL bIsColor ;
00022         char fname[256] ;
00023         FILE *fp ;
00024         
00025         hDevice = dvcOpenCamera(1) ;
00026 
00027         if(hDevice == INVALID_HANDLE_VALUE)
00028         {
00029                 fprintf(stderr,"Error opening DVC Camera!\n");
00030                 return -1 ;
00031         }
00032 
00033         for( i = 1 ; i < argc ; i++ )
00034         {
00035                 if(i+1 < argc && !strcmp(argv[i],"-b")) // Set binning -b 0|1|2|3|4
00036                 {
00037                         int nBin = atoi(argv[++i]) ;
00038                         bRC = dvcSetBinning(hDevice, nBin) ;
00039                         if(!bRC)
00040                                 dvcPrintf(0,"%s: Error setting binning %d\n", argv[0],nBin);
00041                 }
00042                 else if(i+1 < argc && !strcmp(argv[i], "-g"))
00043                 {
00044                         int nGain = atoi(argv[++i]) ;
00045                         bRC = dvcSetGain(hDevice, nGain) ;
00046                         if(!bRC)
00047                                 dvcPrintf(0,"%s: Error setting gain %d\n", argv[0],nGain);
00048                 }
00049                 else if(i+1 < argc && !strcmp(argv[i], "-o"))
00050                 {
00051                         int nOffset = atoi(argv[++i]) ;
00052                         bRC = dvcSetOffset(hDevice, nOffset) ;
00053                         if(!bRC)
00054                                 dvcPrintf(0,"%s: Error setting offset %d\n", argv[0],nOffset);
00055                 }
00056                 else if(i+1 < argc && !strcmp(argv[i], "-e"))
00057                 {
00058                         double dExposeMsec = atof(argv[++i]) ;
00059                         double dReturnExp =     dvcSetExposeMsec(hDevice, dExposeMsec) ;
00060 
00061                         if(dReturnExp != dExposeMsec)
00062                                 dvcPrintf(0,"%s: Set expose to %g [ Not %g ]\n", argv[0], dReturnExp, dExposeMsec);
00063                         
00064                 }
00065         }
00066 
00067 
00068         nStatus = dvcGetStatus(hDevice) ;
00069         nWidth = dvcGetXDim(hDevice) ;
00070         nHeight = dvcGetYDim(hDevice) ;
00071         bIsColor = dvcIsColor(hDevice) ;
00072 
00073 
00074         pRawVideoData = (PUSHORT)calloc(nWidth * nHeight, sizeof(USHORT));
00075         if(NULL == pRawVideoData)
00076         {
00077                 dvcPrintf(0,"%s: Error allocating buffer for raw video.\n");
00078                 dvcCloseCamera(hDevice);
00079                 return -1 ;
00080         }
00081 
00082         if(bIsColor)
00083         {
00084                 pRGBVideoData = (PUSHORT)calloc(nWidth * nHeight,  3 * sizeof(USHORT));
00085                 if(NULL == pRGBVideoData)
00086                 {
00087                         dvcPrintf(0,"%s: Error allocating buffer for RGB video.\n");
00088                         dvcCloseCamera(hDevice);
00089                         free(pRawVideoData);
00090                         return -1 ;
00091                 }
00092         }
00093 
00094 
00095         if(nStatus != DVC_STATUS_RUNNING)       // If not currently streaming video..
00096         {
00097                 bRC = dvcStartSequence(hDevice, 0) ;
00098                 if(!bRC)
00099                 {
00100                         dvcPrintf(0,"%s: Error starting video capture..\n");
00101                         dvcCloseCamera(hDevice);
00102                         if(bIsColor) free(pRGBVideoData);
00103                         free(pRawVideoData);
00104                         return -1 ;
00105                 }
00106         }
00107 
00108         // Wait for a frame 
00109         
00110         bRC = dvcWaitImage(hDevice, 10000.0 ) ; // Wait up to 10 seconds..
00111 
00112 
00113         if(!bRC)
00114         {
00115                 dvcPrintf(0,"%s: Error waiting for video capture..\n");
00116                 dvcCloseCamera(hDevice);
00117                 if(bIsColor) free(pRGBVideoData);
00118                 free(pRawVideoData);
00119                 return -1 ;
00120         }
00121 
00122         // This stops captures.  The driver internally buffers the raw
00123         // data so,  the read functions below return data from
00124         // the last frame captured.
00125         if(nStatus != DVC_STATUS_RUNNING)       
00126                 dvcStopSequence(hDevice) ;
00127 
00128         // Read the raw video ( Bayer ) data..
00129         if(bRC) bRC = dvcReadImage(hDevice, 
00130                                 pRawVideoData,
00131                                 0, 0,                   // Left, top
00132                                 nWidth, nHeight         // width, height
00133                                 ) ;
00134 
00135         if(!bRC)
00136                 dvcPrintf(0,"Error reading raw video!\n");
00137 
00138         // Read the raw video and converts to 48-bits/pixel
00139         // i.e.,  each pixel represented by 3 16-bit values, R,G,B,R,G,B,..
00140         if(bRC && bIsColor)
00141                         bRC = dvcReadImageRGB48(hDevice, 
00142                                         pRGBVideoData,
00143                                         0, 0,                   // Left, top
00144                                         nWidth, nHeight         // width, height
00145                                         ) ;
00146         if(!bRC)
00147                 dvcPrintf(0,"Error reading RGB video!\n");
00148         else 
00149                 fprintf(stdout,"Captured a %d x %d %s Image.\n",
00150                                 nWidth, nHeight,
00151                                 bIsColor ? "Color" : "B&W" ) ;
00152 
00153         if(bRC)
00154         {
00155                 printf("Enter a file name to save the raw image data >") ;
00156                 fgets(fname,255,stdin);
00157                 if(strlen(fname) > 1)
00158                 {
00159                         fname[strlen(fname)-1] = '\0' ;
00160                         fp = fopen(fname,"wb");
00161                         if(fp)
00162                         {
00163                                 fwrite(
00164                                         (void *)pRawVideoData,
00165                                         nWidth * nHeight,
00166                                         sizeof(USHORT),
00167                                         fp 
00168                                         ) ;
00169                                 fclose(fp) ;
00170                         }
00171                 }
00172         }
00173         if(bRC && bIsColor)
00174         {
00175                 printf("Enter a file name to save the RGB image data >") ;
00176                 fgets(fname,255,stdin);
00177                 if(strlen(fname) > 1)
00178                 {
00179                         fname[strlen(fname)-1] = '\0' ;
00180                         fp = fopen(fname,"wb");
00181                         if(fp)
00182                         {
00183                                 fwrite(
00184                                         (void *)pRGBVideoData,
00185                                         nWidth * nHeight,
00186                                         3*sizeof(USHORT),
00187                                         fp 
00188                                         ) ;
00189                                 fclose(fp) ;
00190                         }
00191                 }
00192         }
00193 
00194 
00195         free(pRawVideoData);
00196         if(bIsColor) free(pRGBVideoData);
00197 
00198         dvcCloseCamera(hDevice) ;
00199 
00200 
00201         return 0;
00202 }
00203 

©DVC Company, 2006. All rights reserved.    www.dvcco.com   Created Thu Nov 16 10:07:21 2006