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