Simple API Unpacking and Merging Example Code.
This example shows you how to create a simple image loop and use the API to unpack and merge the image planes.
#include "stdint.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include <string>
#define FLI_TEST_MAX_SUPPORTED_CAMERAS (4)
static int32_t SetFrameInfo(int32_t iDeviceHandle, uint32_t uiWidth, uint32_t uiHeight);
static std::string MakeProcessedFileName(std::string& rstrFileName, uint32_t uiFrameNumber, const char* pSuffix, const char* pExt);
static void SaveProcessedFile(std::string strFileName, uint8_t* pMetaData, uint32_t uiMetaSize, uint8_t* pImageData, uint64_t uiImageByteSize);
static int32_t CheckReferenceMetaData(FILE* pFile);
static int32_t SendHWMergeReferenceFiles(int32_t iHandle, char* pDSNUFileName, char* pPRNUFileName, uint32_t uiWidth, uint32_t uiHeight);
static int32_t s_iDeviceHandle;
uint32_t uiNumDetectedDevices;
static FPRODEVICEINFO s_camDeviceInfo[FLI_TEST_MAX_SUPPORTED_CAMERAS];
int main()
{
int32_t iResult;
int32_t iGrabResult;
uint32_t i;
uint8_t *pFrame;
uint32_t uiFramSizeInBytes;
std::string strFileName;
uint32_t uiCapLength;
pFrame = NULL;
uiNumDetectedDevices = FLI_TEST_MAX_SUPPORTED_CAMERAS;
if ((iResult >= 0) && (uiNumDetectedDevices > 0))
{
s_iDeviceHandle = -1;
iResult =
FPROCam_Open(&s_camDeviceInfo[0], &s_iDeviceHandle);
if ((iResult >= 0) && (s_iDeviceHandle >= 0))
{
memset(&fproUnpacked, 0, sizeof(fproUnpacked));
memset(&fproStats, 0, sizeof(fproStats));
if (iResult >= 0)
if (iResult >= 0)
if (iResult >= 0)
{
pFrame = (uint8_t *)malloc(uiFramSizeInBytes);
if (pFrame)
{
if (iResult >= 0)
{
iGrabResult = 0;
for (i = 0; (i < 10) && (iResult >= 0) && (iGrabResult >= 0); ++i)
{
if (iResult >= 0)
{
#if 1
ShowStats(&fproStats);
#else
#endif
if (iGrabResult >= 0)
{
printf("Got frame %d.\n",i);
#if 1
strFileName = "API_Unpack_Sample.rcd";
{
SaveProcessedFile(MakeProcessedFileName(strFileName, i,
"_MERGED_TIFF",
".tiff"), NULL, 0, (uint8_t*)fproUnpacked.
pMergedImage, fproUnpacked.
uiMergedBufferSize);
SaveProcessedFile(MakeProcessedFileName(strFileName, i,
"_MERGED_FITS",
".fits"), NULL, 0, (uint8_t*)fproUnpacked.
pMergedImage, fproUnpacked.
uiMergedBufferSize);
else
}
#endif
}
}
}
}
}
else
{
printf("ERROR getting frame memory.... leaving\n");
}
}
}
}
else
{
printf("ERROR: No cameras detected!\n\n");
}
if (pFrame)
free(pFrame);
return 0;
}
int DefaultAPIMerging()
{
int32_t iResult;
int32_t iGrabResult;
uint32_t i;
uint8_t* pFrame;
uint32_t uiFramSizeInBytes;
std::string strFileName;
uint32_t uiCapLength;
pFrame = NULL;
uiNumDetectedDevices = FLI_TEST_MAX_SUPPORTED_CAMERAS;
if ((iResult >= 0) && (uiNumDetectedDevices > 0))
{
s_iDeviceHandle = -1;
iResult =
FPROCam_Open(&s_camDeviceInfo[0], &s_iDeviceHandle);
if ((iResult >= 0) && (s_iDeviceHandle >= 0))
{
memset(&fproUnpacked, 0, sizeof(fproUnpacked));
memset(&fproStats, 0, sizeof(fproStats));
if (iResult >= 0)
if (iResult >= 0)
{
pFrame = (uint8_t*)malloc(uiFramSizeInBytes);
if (pFrame)
{
if (iResult >= 0)
{
iGrabResult = 0;
for (i = 0; (i < 10) && (iResult >= 0) && (iGrabResult >= 0); ++i)
{
if (iResult >= 0)
{
#if 0
#else
#endif
if (iGrabResult >= 0)
{
printf("Got frame %d.\n", i);
strFileName = "API_Unpack_Sample.rcd";
}
}
}
}
}
else
{
printf("ERROR getting frame memory.... leaving\n");
}
}
}
}
if (pFrame)
free(pFrame);
return 0;
}
int32_t
SetFrameInfo(int32_t iDeviceHandle, uint32_t uiWidth, uint32_t uiHeight)
{
int32_t iResult;
iResult = 0;
if (iResult >= 0)
if (iResult >= 0)
return(iResult);
}
std::string MakeProcessedFileName(std::string & rstrFileName, uint32_t uiFrameNumber, const char* pSuffix, const char* pExt)
{
std::string strNew;
std::string strExt;
char cNumBuff[32];
size_t iIndex;
iIndex = rstrFileName.find_last_of('.', rstrFileName.length());
if (iIndex != std::string::npos)
{
strNew = rstrFileName.substr(0,iIndex);
snprintf(cNumBuff, sizeof(cNumBuff), "_%d_", uiFrameNumber);
strNew += cNumBuff;
strNew += pSuffix;
}
else
{
strNew = rstrFileName + pSuffix;
}
strNew += pExt;
return(strNew);
}
void SaveProcessedFile(std::string rstrFileName, uint8_t* pMetaData, uint32_t uiMetaSize, uint8_t* pImageData, uint64_t uiImageByteSize)
{
FILE* pFile;
int32_t iResult;
#if defined(_WIN32) || defined(_WINDOWS)
fopen_s(&pFile, rstrFileName.c_str(), "w+b");
#else
pFile= fopen(rstrFileName.c_str(), "w+b");
#endif
if (pFile)
{
iResult = 0;
if (pMetaData)
{
if (fwrite(pMetaData, 1, uiMetaSize, pFile) != uiMetaSize)
{
printf("Error writing Meta Data to file %s\n", rstrFileName.c_str());
iResult = -1;
}
}
if (iResult >= 0)
{
if (fwrite(pImageData, 1, (size_t)uiImageByteSize, pFile) != uiImageByteSize)
{
printf("Error writing Image Data to file %s\n", rstrFileName.c_str());
}
}
fclose(pFile);
}
else
{
printf("Error opening output file %s\n", rstrFileName.c_str());
}
}
int32_t CheckReferenceMetaData(FILE* pFile)
{
int32_t iResult;
size_t uiBytesRead;
uint32_t uiMetaSize;
uint32_t uiFileSize;
uint8_t uiMetaCheck[6];
iResult = -1;
fseek(pFile, 0L, SEEK_END);
uiFileSize = ftell(pFile);
fseek(pFile, 0L, SEEK_SET);
#if defined(_WIN32) || defined(_WINDOWS)
uiBytesRead = fread_s(uiMetaCheck, 6, 1, 6, pFile);
#else
uiBytesRead= fread(uiMetaCheck, 1, 6, pFile);
#endif
if (uiBytesRead == 6)
{
if ((uiMetaCheck[0] == 'M') && (uiMetaCheck[1] == 'e') && (uiMetaCheck[2] == 't') && (uiMetaCheck[3] == 'a'))
{
uiMetaSize = ((uint32_t)uiMetaCheck[4] & 0xFF) << 8;
uiMetaSize |= (uint32_t)uiMetaCheck[5] & 0xFF;
if (uiMetaSize < uiFileSize)
{
iResult= 0;
fseek(pFile, uiMetaSize, SEEK_SET);
}
}
}
return(iResult);
}
{
if (pStats)
{
printf("Mean: %f\n"
"Median: %f\n"
"Mode: %f\n"
"Std Dev: %f\n"
"Brightest: %d, (%d,%d)\n"
"Dimmest: %d, (%d,%d)\n",
}
else
{
printf("Unallocated\n");
}
}
{
if (NULL == pStats)
return;
printf("Low Stats: ");
printf("Not Requested\n");
else
{
printf("\n");
}
printf("\nHigh Stats: ");
printf("Not Requested\n");
else
{
printf("\n");
}
printf("\nMerged Stats: ");
printf("Not Requested\n");
else
{
printf("\n");
}
printf("\n");
}
int32_t SendHWMergeReferenceFiles(int32_t iHandle, char* pDSNUFileName, char* pPRNUFileName, uint32_t uiWidth, uint32_t uiHeight)
{
int32_t iResult;
FILE* pDSNUFile;
FILE* pPRNUFile;
uint32_t uiFrameSize;
size_t uiBytesRead;
iResult = -1;
pDSNUFile = NULL;
pPRNUFile = NULL;
#if defined(_WIN32) || defined(_WINDOWS)
fopen_s(&pDSNUFile, pDSNUFileName, "r+b");
fopen_s(&pPRNUFile, pPRNUFileName, "r+b");
#else
pDSNUFile= fopen(pDSNUFileName, "r+b");
pPRNUFile= fopen(pPRNUFileName, "r+b");
#endif
iResult = CheckReferenceMetaData(pDSNUFile);
if (iResult >= 0)
iResult = CheckReferenceMetaData(pPRNUFile);
if (iResult >= 0)
{
iResult = 0;
{
uiFrameSize = uiWidth * uiHeight * sizeof(uint16_t);
#if defined(_WIN32) || defined(_WINDOWS)
uiBytesRead = fread_s(refFrames.
pAdditiveLowGain, uiFrameSize, 1, uiFrameSize, pDSNUFile);
#else
#endif
if (uiBytesRead != uiFrameSize)
iResult = -1;
if (iResult >= 0)
{
#if defined(_WIN32) || defined(_WINDOWS)
uiBytesRead = fread_s(refFrames.
pAdditiveHighGain, uiFrameSize, 1, uiFrameSize, pDSNUFile);
#else
#endif
if (uiBytesRead != uiFrameSize)
iResult = -1;
}
if (iResult >= 0)
{
#if defined(_WIN32) || defined(_WINDOWS)
#else
#endif
if (uiBytesRead != uiFrameSize)
iResult = -1;
}
if (iResult >= 0)
{
#if defined(_WIN32) || defined(_WINDOWS)
#else
#endif
if (uiBytesRead != uiFrameSize)
iResult = -1;
}
if (iResult >= 0)
{
}
}
else
{
iResult = -1;
}
if (pDSNUFile)
fclose(pDSNUFile);
if (pPRNUFile)
fclose(pPRNUFile);
}
return(iResult);
}