Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gain file loading and saving using consistent with vendor header format #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 79 additions & 22 deletions perkinElmerApp/src/PerkinElmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,9 +1488,6 @@ void PerkinElmer::acquireStopTask(void)
/** Saves a gain file */
asynStatus PerkinElmer::saveGainFile(void)
{
int iSizeX;
int iSizeY;
int iByteDepth;
int status = asynSuccess;
char gainPath[256];
char gainFile[256];
Expand All @@ -1504,15 +1501,14 @@ asynStatus PerkinElmer::saveGainFile(void)
status |= getStringParam(PE_CorrectionsDirectory, sizeof(gainPath), gainPath);
status |= getStringParam(PE_GainFile, sizeof(gainFile), gainFile);
strcat(gainPath, gainFile);
status |= getIntegerParam(NDArraySizeX, &iSizeX);
status |= getIntegerParam(NDArraySizeY, &iSizeY);

if (pGainBuffer_ == NULL) return asynError;

asynPrint(pasynUserSelf, ASYN_TRACE_FLOW,
"%s:%s:, saving gain file: %s\n",
driverName, functionName, gainPath);


pOutputFile = fopen (gainPath, "wb");

if (pOutputFile == NULL) {
Expand All @@ -1521,11 +1517,22 @@ asynStatus PerkinElmer::saveGainFile(void)
driverName, functionName, gainFile);
return asynError;
}
iByteDepth = sizeof (DWORD);

fwrite ((void *) &iSizeX, sizeof (int), 1, pOutputFile);
fwrite ((void *) &iSizeY, sizeof (int), 1, pOutputFile);
fwrite ((void *) &iByteDepth, sizeof (int), 1, pOutputFile);
// Write fileHeader = 68 bytes
fwrite((void *) &fileHeader_.FileType,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.HeaderSize,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.HeaderVersion,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.FileSize,sizeof(ULONG),1,pOutputFile);
fwrite((void *) &fileHeader_.ImageHeaderSize,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.ULX,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.ULY,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.BRX,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.BRY,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.NrOfFrames,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.Correction,sizeof(WORD),1,pOutputFile);
fwrite((void *) &fileHeader_.IntegrationTime,sizeof(double),1,pOutputFile);
fwrite((void *) &fileHeader_.TypeOfNumbers,sizeof(WORD),1,pOutputFile);

if (ferror (pOutputFile)) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to write file header for file %s\n",
Expand All @@ -1534,12 +1541,21 @@ asynStatus PerkinElmer::saveGainFile(void)
return asynError;
}

fwrite (pGainBuffer_, iByteDepth, iSizeX*iSizeY, pOutputFile);
// Write Image Header = 32 bytes
fwrite((void*)&imageHeader_,fileHeader_.ImageHeaderSize,1, pOutputFile);
if (ferror (pOutputFile)) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to write data for file %s\n",
driverName, functionName, gainFile);
fclose (pOutputFile);
"%s:%s: Failed to read image header for gain correction file %s\n",
driverName, functionName, gainPath);
return asynError;
}

// Write gain image
fwrite (pGainBuffer_, (fileHeader_.TypeOfNumbers/8) * fileHeader_.BRX * fileHeader_.BRY, 1, pOutputFile);
if (ferror (pOutputFile)) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to read data for gain correction file %s\n",
driverName, functionName, gainPath);
return asynError;
}

Expand All @@ -1551,7 +1567,6 @@ asynStatus PerkinElmer::saveGainFile(void)
return asynSuccess;
}


//_____________________________________________________________________________________________

/** Loads a gain file */
Expand All @@ -1560,7 +1575,6 @@ asynStatus PerkinElmer::loadGainFile (void)
int status = asynSuccess;
char gainPath[256];
char gainFile[256];
int iSizeX, iSizeY, iByteDepth;
FILE *pInputFile;
struct stat stat_buffer;
static const char *functionName = "loadGainFile";
Expand All @@ -1582,27 +1596,71 @@ asynStatus PerkinElmer::loadGainFile (void)
driverName, functionName, gainPath);
return asynError;
}

if (pGainBuffer_ != NULL)
free (pGainBuffer_);

pInputFile = fopen (gainPath, "rb");

if (pInputFile == NULL) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to open gain correction file %s\n",
driverName, functionName, gainPath);
return asynError;
}
fread (&iSizeX, sizeof (int), 1, pInputFile);
fread (&iSizeY, sizeof (int), 1, pInputFile);
fread (&iByteDepth, sizeof (int), 1, pInputFile);

// Read fileHeader = 68 bytes
fread(&fileHeader_.FileType,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.HeaderSize,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.HeaderVersion,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.FileSize,sizeof(ULONG),1,pInputFile);
fread(&fileHeader_.ImageHeaderSize,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.ULX,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.ULY,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.BRX,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.BRY,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.NrOfFrames,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.Correction,sizeof(WORD),1,pInputFile);
fread(&fileHeader_.IntegrationTime,sizeof(double),1,pInputFile);
fread(&fileHeader_.TypeOfNumbers,sizeof(WORD),1,pInputFile);

if (ferror (pInputFile)) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to read file header for gain correction file %s\n",
driverName, functionName, gainPath);
return asynError;
}
pGainBuffer_ = (DWORD *) malloc (iSizeX * iSizeY * iByteDepth);
fread (pGainBuffer_, iByteDepth, iSizeX * iSizeY, pInputFile);

// We were having problems because the file_header structure was not read properly.
// That is why these debug printf statements were added
/*
printf("FileType=%x\n", fileHeader_.FileType);
printf("HeaderSize=%d\n", fileHeader_.HeaderSize);
printf("HeaderVersion=%d\n", fileHeader_.HeaderVersion);
printf("FileSize=%d\n", fileHeader_.FileSize);
printf("ImageHeaderSize=%d\n", fileHeader_.ImageHeaderSize);
printf("ULX=%d, ULY=%d\n", fileHeader_.ULX, fileHeader.ULY);
printf("BRX=%d, BRY=%d\n", fileHeader_.BRX, fileHeader.BRY);
printf("NrOfFrames=%d\n", fileHeader_.NrOfFrames);
printf("Correction=%d\n", fileHeader_.Correction);
printf("IntegrationTime=%f\n", fileHeader_.IntegrationTime);
printf("TypeOfNumbers=%d\n", fileHeader_.TypeOfNumbers);
printf("sizeof(TypeOfNumbers)=%d\n", sizeof(fileHeader.TypeOfNumbers));
*/

// Read Image Header = 32 bytes
fread((void*)&imageHeader_,fileHeader_.ImageHeaderSize,1, pInputFile);
if (ferror (pInputFile)) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to read image header for gain correction file %s\n",
driverName, functionName, gainPath);
return asynError;
}

pGainBuffer_ = (DWORD *) malloc (fileHeader_.BRX * fileHeader_.BRY * (fileHeader_.TypeOfNumbers/8));

// Read gain image
fread (pGainBuffer_, (fileHeader_.TypeOfNumbers/8) * fileHeader_.BRX * fileHeader_.BRY, 1, pInputFile);
if (ferror (pInputFile)) {
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: Failed to read data for gain correction file %s\n",
Expand All @@ -1611,16 +1669,15 @@ asynStatus PerkinElmer::loadGainFile (void)
}

fclose (pInputFile);

status |= setIntegerParam(PE_GainAvailable, 1);
callParamCallbacks();

asynPrint(pasynUserSelf, ASYN_TRACE_FLOW,
"%s:%s:, Gain file %s loaded\n",
driverName, functionName, gainPath);
return asynSuccess;

}

//_____________________________________________________________________________________________

/** Loads a pixel correction file */
Expand Down
3 changes: 3 additions & 0 deletions perkinElmerApp/src/PerkinElmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class PerkinElmer : public ADDriver
bool acquireSettingsChanged_;
bool doSoftwareTriggers_;

WinHeaderType fileHeader_;
WinImageHeaderType imageHeader_;

bool initializeDetector (void);
void setBinning(void);
void reportSensors(FILE *fp, int details);
Expand Down