#define STRICT #define GLOBAL__ #include #include "go1.h" ULNG ulHeight = 0L; ULNG ulWidth = 0L; HGLOBAL hAcquireBuf = NULL; HPUCHR hpAcquireBuf = NULL; OLT_FG_FRAME_ID FrameId = 0; /* These are the pointer to the function related to the Dt3157 */ FARPROC Dt3157EnableExposureMode; FARPROC Dt3157SetDigitalCameraType; FARPROC Dt3157SetDigitalIOConfiguration; FARPROC Dt3157SetDigitalIO; FARPROC Dt3157SetSyncMasterControlValue; FARPROC Dt3157SetExposure; FARPROC Dt3157EnableSyncMasterMode; FARPROC Dt3157QueryDigitalCameraType; WORD DrawRGBFrame (HWND hWnd, VOID* FrameAddr, ULNG Width, ULNG Height); #define VSRC0 0 // Video source BOOL RGBDataFlag; // Prototypes internal to this module BOOL AsciiHexToDec (char * szBuff, ULNG * ulDecVal); /**********************************************************/ /*========================================================*/ /*================= main function ====================*/ /*========================================================*/ /**********************************************************/ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char Alias[40], good[40], name[40]; BOOL bGotAlias = 0; int jjj, NFrame; BOOL error = FALSE; sscanf (lpCmdLine, "%s %s %d", Alias, good, &NFrame); bGotAlias = InitDeviceList(); if (!bGotAlias) { FILE *file_out; file_out = fopen ("ERROR.txt", "w"); fprintf (file_out, "InitDeviceList sucks weiner\n"); return 0; } if (!NewDevice(Alias)) { FILE *file_out; file_out = fopen ("ERROR.txt", "w"); fprintf (file_out, "didn't get good with NewDevice\n"); return 0; } for (jjj = 0; jjj < NFrame; jjj++) { sprintf (name, "%s%d.sba", good, jjj); error = AcquireToHost(CurDevInfo, name); if (error) return 0; } return 0; } /****************************************************************/ /*==============================================================*/ /*============== Get Device Alias ==========================*/ /*==============================================================*/ /****************************************************************/ BOOL PRIVATE InitDeviceList() { int iCount = 0; OLT_APISTATUS Status = OLC_STS_NORMAL; HGLOBAL hDevInfoList = NULL; LPOLT_IMGDEVINFO lpDevInfoList = NULL; /* How many aliases do we have */ Status = OlImgGetDeviceCount(&iCount); /* Allocate space for info */ hDevInfoList = GlobalAlloc(GHND, iCount * sizeof(OLT_IMGDEVINFO)); lpDevInfoList = (LPOLT_IMGDEVINFO) GlobalLock(hDevInfoList); if ( !lpDevInfoList ) /* Fill in the device info */ lpDevInfoList->StructSize = sizeof(OLT_IMGDEVINFO); Status = OlImgGetDeviceInfo(lpDevInfoList, iCount * sizeof(OLT_IMGDEVINFO)); GlobalUnlock(hDevInfoList); GlobalFree(hDevInfoList); return TRUE; } /****************************************************************/ /*==============================================================*/ /*================ Close Device ============================*/ /*==============================================================*/ /****************************************************************/ void PRIVATE CloseCurrentDevice(void) { /* Close current device */ ulHeight = 0L; ulWidth = 0L; GlobalUnlock(hAcquireBuf); GlobalFree(hAcquireBuf); hAcquireBuf = NULL; hpAcquireBuf = NULL; if (CurDevInfo.DevId != NULL) (void) OlImgCloseDevice(CurDevInfo.DevId); /* Clear the device info structures */ _fmemset((LPVOID)&CurDevCaps, 0, sizeof(DEVCAPS)); _fmemset((LPVOID)&CurDevInfo, 0, sizeof(DEVINFO)); } /****************************************************************/ /*==============================================================*/ /*============== Open Device Info ===========================*/ /*==============================================================*/ /****************************************************************/ BOOL PRIVATE NewDevice(LPCSTR lpcszAlias) { OLT_IMG_DEV_ID DevId = NULL; OLT_APISTATUS Status = OLC_STS_NORMAL; LPULNG lpLutData = NULL; HLOCAL hLutData = NULL; ULNG ulResult = 0L; ULNG ulLutCount = 0L; ULNG ulMaxIndex = 0L; ULNG ulMaxValue = 0L; ULNG ulOldData = 0L; ULNG ulPixelDepth = 0L; ULNG i, j; UINT uStep; STRING DeviceName = {0}; /* Try to open the device */ if ( ! OlImgIsOkay( OlImgOpenDevice(lpcszAlias, &DevId) ) ) return FALSE; CurDevInfo.DevId = DevId; CurDevInfo.InputSource = 0; /************************************************************************** This section has been added to detect a DT3157 board to allow the user to supply a *.cam file to configure the board for the current digital camera used. We query the driver for the kind of board that has been opened. (No matter the previous ones). We query for the key "device name". If the board is a DT3157, then we launch do the following: - load the DT315732.DLL that contains the 3157 specific functions - check if the loader succeeded - ask for the address of the functions we need - check if the DLL contained all the functions we needed - launch the dialog box and then supply the file name to the configuration routine. **************************************************************************/ OlImgQueryDeviceCaps(CurDevInfo.DevId, OLC_IMG_DC_DEVICE_NAME,DeviceName,sizeof(STRING)); if (!strcmp("DT3157",DeviceName)) { HINSTANCE DllHandle = NULL; // Open the DLL DllHandle = LoadLibrary("DT315732.DLL"); if (DllHandle) // Did the loader succeed { // If yes, get the addresses of the functions Dt3157EnableExposureMode = GetProcAddress(DllHandle,"Dt3157EnableExposureMode"); Dt3157SetDigitalCameraType = GetProcAddress(DllHandle,"Dt3157SetDigitalCameraType"); Dt3157SetDigitalIOConfiguration = GetProcAddress(DllHandle,"Dt3157SetDigitalIOConfiguration"); Dt3157SetDigitalIO = GetProcAddress(DllHandle,"Dt3157SetDigitalIO"); Dt3157SetSyncMasterControlValue = GetProcAddress(DllHandle,"Dt3157SetSyncMasterControlValue"); Dt3157SetExposure = GetProcAddress(DllHandle,"Dt3157SetExposure"); Dt3157EnableSyncMasterMode = GetProcAddress(DllHandle,"Dt3157EnableSyncMasterMode"); Dt3157QueryDigitalCameraType = GetProcAddress(DllHandle,"Dt3157QueryDigitalCameraType"); if (!( Dt3157EnableExposureMode && Dt3157SetDigitalCameraType && Dt3157SetDigitalIOConfiguration && Dt3157SetDigitalIO && Dt3157SetSyncMasterControlValue && Dt3157SetExposure && Dt3157EnableSyncMasterMode && Dt3157QueryDigitalCameraType) ) // One of those failed ? { // Yes, display a message box and exit /* MessageBox ( NULL, "You do not have the correct version of DT315732.DLL", "Error loading function", MB_APPLMODAL );*/ exit(0); } } else { // No, display a message box and exit /*MessageBox ( NULL, "File dt315732.dll not found", "Error loading DLL", MB_APPLMODAL );*/ exit(0); } //Status = WriteCameraSetupToBoard (hWnd); /*MyPrintf("Loading Camera File Status = %x",Status);*/ if (Status == OLC_STS_CANCELJOB) { if (CurDevInfo.DevId != NULL) Status = OlImgCloseDevice(CurDevInfo.DevId); /* Clear the device info structures */ if(Status == OLC_STS_NORMAL) { _fmemset((LPVOID)&CurDevCaps, 0, sizeof(DEVCAPS)); _fmemset((LPVOID)&CurDevInfo, 0, sizeof(DEVINFO)); } return FALSE; } } if (!strcmp("DT3153",DeviceName)) { RGBDataFlag=TRUE; } //************************************************************************** //************************************************************************** /* Got it, now query to see which sections are supported by the device. This information is stored in the GLOBAL CurDevCaps and is used to enable/disable menus in WM_INITMENUPOPUP */ Status = OlImgQueryDeviceCaps(DevId, OLC_IMG_DC_SECTIONS, &ulResult, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Check to see if the board has an INPUT Section. All devices should have an INPUT section but the same sort of check would be needed to determine if the device had CAMERA CTL, LINEAR MEMORY or on board MEMORY sections. */ if (! (ulResult & OLC_FG_SECTION_INPUT)) { CloseCurrentDevice(); return FALSE; } /* Okay the board has Input Capabilities, now check for ILUT's */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_ILUT_COUNT, &ulLutCount, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Do we have at least 1 ILUT? */ if ( ulLutCount > 0 ) { /* Check the max ILUT index */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_MAX_ILUT_INDEX, &ulMaxIndex, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Get the number of bytes per pixel for loading the ILUT */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_PIXEL_DEPTH, &ulPixelDepth, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Allocate the array to store the ILUT values */ hLutData = LocalAlloc(LHND, (UINT)((ulMaxIndex + 1) * sizeof(ULNG)) ); if (!hLutData) { CloseCurrentDevice(); return FALSE; } lpLutData = (LPULNG) LocalLock(hLutData); if (!lpLutData) { LocalFree(hLutData); CloseCurrentDevice(); return FALSE; } /* Check the max value for an ILUT entry */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_MAX_ILUT_VALUE, &ulMaxValue, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { LocalUnlock(hLutData); LocalFree(hLutData); CloseCurrentDevice(); return FALSE; } /* Calculate the increment step for the case that there are more Indexes than Values in the table (i.e. 16 bit to 8 bit). In most cases this value will be 1 since the number of table Indexes is usually equal to the Max possible value (i.e. 16 bit to 16 bit or 8 bit to 8 bit). */ uStep = (UINT)(ulMaxIndex / ulMaxValue); /* Once the step is calculated the ILUT will be loaded as follows: 0 ---------- | 0 | | . | | . | | . | 1 * step--------| | 1 | | . | | . | | . | 2 * step--------| | 2 | | . | | . | | . | 3 * step--------| | 3 | | . | | . | | . | ---------- */ for (i = 0; i < ulMaxIndex; i += uStep) for (j = 0; j < uStep; j++) /* Load with the identity */ lpLutData[i + j] = i / uStep; /* Write the values to ILUT 0. */ Status = OlFgWriteInputLUT(DevId, 0, 0, ulMaxIndex, lpLutData); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { LocalUnlock(hLutData); LocalFree(hLutData); CloseCurrentDevice(); return FALSE; } /* Query for the current Video Input Source for setting the ILUT input control value */ Status = OlFgQueryInputVideoSource(DevId, &CurDevInfo.InputSource); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { LocalUnlock(hLutData); LocalFree(hLutData); CloseCurrentDevice(); return FALSE; } /* Tell the board to use ILUT 0 */ Status = OlFgSetInputControlValue(DevId, CurDevInfo.InputSource, OLC_FG_CTL_ILUT, 0, &ulOldData); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { LocalUnlock(hLutData); LocalFree(hLutData); CloseCurrentDevice(); return FALSE; } /* Free the memory */ LocalUnlock(hLutData); LocalFree(hLutData); } /* end if ILUT > 0 */ /* Query input filter caps */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_INPUT_FILTER, &CurDevCaps.DoesInputFilter, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Query A/D */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_PROG_A2D, &CurDevCaps.DoesA2D, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Query video type stuff */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_VIDEO_SELECT, &CurDevCaps.DoesVideoType, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Query active video area stuff */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_ACTIVE_VIDEO, &CurDevCaps.DoesActiveVideo, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Query frame size stuff */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_FRAME_SELECT, &CurDevCaps.DoesFrameSize, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Query clock stuff */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_PIXEL_CLOCK, &CurDevCaps.DoesClock, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) && (Status != OLC_STS_UNSUPKEY) ) { CloseCurrentDevice(); return FALSE; } /* Query frame type stuff */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_FRAME_TYPE_LIMITS, &CurDevCaps.FrameTypes, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) ) { CloseCurrentDevice(); return FALSE; } /* Query Sync Sentinel stuff */ Status = OlFgQueryInputCaps(DevId, OLC_FG_IC_DOES_SYNC_SENTINEL, &CurDevCaps.DoesSyncSentinel, sizeof(BOOL)); if ( ! OlImgIsOkay(Status) ) { CloseCurrentDevice(); return FALSE; } /* Query Input Source Channels */ Status = OlFgQueryInputCaps(CurDevInfo.DevId, OLC_FG_IC_INPUT_SOURCE_COUNT, &CurDevCaps.SourceCount, sizeof(ULNG)); if ( ! OlImgIsOkay(Status) ) { CloseCurrentDevice(); return FALSE; } return TRUE; } /****************************************************************/ /*==============================================================*/ /*================ Acquire Frame ===========================*/ /*==============================================================*/ /****************************************************************/ BOOL PRIVATE AcquireToHost(DEVINFO DevInfo, char *go) { int num_pixels; OLT_APISTATUS Status = 0L; OLT_APISTATUS Stat = 0L; ULNG ulPixelDepth = 0L; ULNG ulMinBufSize = 0L; OLT_FG_TRIGGER OldTrigger = 0; OLT_FG_TRIGGER NewTrigger = 0; BOOL bWasTrigger = FALSE; BOOL bTriggerOnLowToHigh = FALSE; OLT_FG_FRAME_INFO *Frame; FILE *buf; buf = fopen (go, "wb"); Frame = GlobalAlloc (GHND, sizeof(OLT_FG_FRAME_INFO)); if (!Frame) { FILE *file_out; file_out = fopen ("ERROR.txt", "w"); fprintf(file_out, "Unable to allocate Frame memory"); return (TRUE); } // if using RGB drawing release old resources from last acquire if((RGBDataFlag) && (ulHeight)) { ulHeight = 0L; ulWidth = 0L; GlobalUnlock(hAcquireBuf); GlobalFree(hAcquireBuf); } hAcquireBuf = NULL; hpAcquireBuf = NULL; /* Query for Height, Width and Pixel Depth of the frame */ (void) OlFgQueryInputControlValue(DevInfo.DevId, DevInfo.InputSource, OLC_FG_CTL_FRAME_HEIGHT, &ulHeight); (void) OlFgQueryInputControlValue(DevInfo.DevId, DevInfo.InputSource, OLC_FG_CTL_FRAME_WIDTH, &ulWidth); (void) OlFgQueryInputCaps(DevInfo.DevId, OLC_FG_IC_PIXEL_DEPTH, &ulPixelDepth, sizeof(ULNG)); /* calculate minimum size of buffer in bytes */ ulMinBufSize = ulHeight * ulWidth * ulPixelDepth; hAcquireBuf = GlobalAlloc(GHND, ulMinBufSize); if ( !hAcquireBuf ) { /* unable to allocate memory */ FILE *file_out; file_out = fopen ("ERROR.txt", "w"); fprintf(file_out, "Unable to allocate enough memory for acquire."); return (TRUE); } hpAcquireBuf = (HPUCHR) GlobalLock(hAcquireBuf); if ( !hpAcquireBuf) { /* unable to lock memory */ FILE *file_out; file_out = fopen ("ERROR.txt", "w"); fprintf(file_out, "Unable to lock memory for acquire."); return (TRUE); } Status = OlFgAllocateBuiltInFrame ( DevInfo.DevId, OLC_FG_DEV_MEM_VOLATILE, OLC_FG_NEXT_FRAME, &FrameId); if ( !OlImgIsOkay(Status)) { PrintStatus(NULL, Status, "Unable to Allocate Frame.", "Acquire Error"); } Status = OlFgSetTriggerInfo(DevInfo.DevId, OLC_FG_TRIGGER_EXTERNAL_LINE,bTriggerOnLowToHigh, &OldTrigger, &bWasTrigger); /* Have the memory, attempt the acquire */ Status = OlFgAcquireFrameToHost(DevInfo.DevId, FrameId, hpAcquireBuf, ulMinBufSize); if ( !OlImgIsOkay(Status)) { PrintStatus(NULL, Status, "Unable to acquire.", "Acquire Error"); (void) OlFgDestroyFrame( DevInfo.DevId, FrameId); GlobalUnlock(hAcquireBuf); GlobalFree(hAcquireBuf); return (TRUE); } Stat = OlFgMapFrame (DevInfo.DevId, FrameId, Frame); num_pixels = (int) (Frame->Width * Frame->Height); fwrite ((Frame->BaseAddress), 1, num_pixels, buf); fclose (buf); Stat = OlFgUnmapFrame (DevInfo.DevId, FrameId, Frame->BaseAddress); GlobalFree (Frame); /* create a DIB and display it in the client area */ if(!RGBDataFlag) { GlobalUnlock(hAcquireBuf); GlobalFree(hAcquireBuf); } /* we no longer need the frame, free it */ Status = OlFgDestroyFrame( DevInfo.DevId, FrameId); /*if ( !OlImgIsOkay(Status)) { PrintStatus(NULL, Status, "Unable to free frame after acquire.", "Acquire Error"); }*/ return (FALSE); } BOOL AsciiHexToDec (char * szBuff, ULNG * ulDecVal) { char szStr[16]; int iCt, iCCt, iTest; double dblSum = 0; iCt = strlen (szBuff); // check for valid hex character... for (iCCt=0; iCCt < iCt; iCCt++) { szStr[0] = szBuff[iCCt]; szStr[1] = '\0'; // if character is zero or non-int... if (atoi (szStr) == 0) { // test for actual zero... if (szStr[0] == '0') iTest = 0; else { // test for invalid character... iTest = (int)(szStr[0] & 0xBF); if (iTest > 6) // return failure if invalid character is found return FALSE; else // convert A thru F to 10 thru 15 iTest = iTest+9; } } else { // character is 1 thru 9... iTest = atoi(szStr); } dblSum = dblSum + (double)iTest * pow ( 16., (double)(iCt - iCCt -1) ); } * ulDecVal = (long)dblSum; return TRUE; }