2 ***********************************************************
\r
3 $SNARK_Header: S N A R K 1 4 - A PICTURE RECONSTRUCTION PROGRAM $
\r
4 $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/recfile.cpp $
\r
5 $LastChangedRevision: 118 $
\r
6 $Date: 2014-07-09 14:22:29 -0400 (Wed, 09 Jul 2014) $
\r
8 ***********************************************************
\r
16 #include "int2str.h"
\r
17 #include "DIGFile.h"
\r
19 #include "recfile.h"
\r
21 SnarkRecFile RecFile;
\r
23 void SnarkRecFile::FlipImage(REAL* Dst, REAL* Src)
\r
26 REAL* SrcPtr = Src; // first row
\r
27 REAL* DstPtr = Dst + NumberOfElements * (NumberOfElements - 1); // last row
\r
29 for (i = 0; i < NumberOfElements; i++)
\r
33 memcpy(DstPtr, SrcPtr, NumberOfElements * sizeof(REAL));
\r
35 DstPtr -= NumberOfElements; // set to previous row
\r
36 SrcPtr += NumberOfElements; // set to next row
\r
40 INTEGER SnarkRecFile::Open(const CHAR* FileName, INTEGER nelem, REAL pixsiz)
\r
42 // opens a file for writing
\r
43 // and sets main header values
\r
45 NumberOfElements = nelem;
\r
47 // allocate data buffer
\r
48 DataBuffer = new REAL[NumberOfElements * NumberOfElements];
\r
52 if (DIGFileSnarkRec::Open(FileName, "Proj Name", nelem, pixsiz, "This reconstruction file has been created by snark14") != 0)
\r
59 INTEGER SnarkRecFile::Open(const CHAR* FileName)
\r
61 // opens a file for reading
\r
63 if (DIGFileSnarkRec::Open(FileName) != 0)
\r
68 if (GetSampling(&PixelSize) != 0)
\r
73 if (GetDimensions(&NumberOfElements) != 0)
\r
78 DataBuffer = new REAL[NumberOfElements * NumberOfElements];
\r
86 INTEGER SnarkRecFile::GetNelem(INTEGER* nelem)
\r
89 // returns the value of nelem
\r
91 *nelem = NumberOfElements;
\r
96 INTEGER SnarkRecFile::GetPixSize(REAL* pixsize)
\r
99 // returns the value of nelem
\r
101 *pixsize = PixelSize;
\r
105 INTEGER SnarkRecFile::GetProjName(CHAR pProjName[81])
\r
107 // returns the projection name
\r
109 const CHAR* ProjName;
\r
111 if (DIGFileSnarkRec::GetProjName(&ProjName) != 0)
\r
116 strncpy(pProjName, ProjName, 80);
\r
123 INTEGER SnarkRecFile::SetProjName(CHAR* pProjName)
\r
125 // changes the projection name (already set when opening file for writing)
\r
126 return DIGFileSnarkRec::SetProjName(pProjName);
\r
129 INTEGER SnarkRecFile::WritePhan(CHAR* pPhanName, REAL* pData)
\r
131 // flip image to DIGFile order
\r
132 FlipImage(DataBuffer, pData);
\r
134 // append to DIGFile
\r
135 return AppendPhantom(pPhanName, "Phantom created by snark14", DataBuffer);
\r
138 INTEGER SnarkRecFile::WriteRec(CHAR* pRecName, CHAR pAlgName[4],
\r
139 unsigned INTEGER pCount, unsigned INTEGER pIter, REAL* pData)
\r
142 // writes a reconstruction array
\r
143 // opens a new array set if necessary
\r
145 // trim string for 3 letter algorithm names
\r
146 if (pAlgName[3] == ' ')
\r
151 // convert to upper case
\r
152 for (int i = 0; i < 4; i++)
\r
154 pAlgName[i] = toupper(pAlgName[i]);
\r
159 if (AppendRecSet(pAlgName, pRecName, "") != 0)
\r
165 // flip image to DIGFile order
\r
166 FlipImage(DataBuffer, pData);
\r
168 if (AppendRec(pCount, "Reconstruction created by snark14", DataBuffer) != 0)
\r
176 INTEGER SnarkRecFile::ReadPhan(CHAR pPhantomName[81], REAL* pData)
\r
178 // reads a phatom array (first array set)
\r
179 // if none exists, returns 2
\r
181 const CHAR* PhantomName;
\r
183 if (!PhantomPresent)
\r
184 { // no phantom in file
\r
188 if (GetPhantomName(&PhantomName) != 0)
\r
193 strncpy(pPhantomName, PhantomName, 80);
\r
195 pPhantomName[80] = 0;
\r
197 if (GetPhantomData(DataBuffer) != 0)
\r
202 // flip image to Snark order
\r
203 FlipImage(pData, DataBuffer);
\r
205 unsigned int ArraySize;
\r
207 GetArrayNoOfItems(&ArraySize);
\r
209 ApplyLimits(ArraySize, pData);
\r
214 INTEGER SnarkRecFile::ApplyLimits(unsigned int pArraySize, REAL* pData)
\r
222 for (i = 0; i < pArraySize; i++)
\r
224 if (pData[i] < Modefl.lower)
\r
225 pData[i] = Modefl.lower;
\r
226 if (pData[i] > Modefl.upper)
\r
227 pData[i] = Modefl.upper;
\r
232 for (i = 0; i < pArraySize; i++)
\r
234 if (pData[i] < Modefl.lower)
\r
235 pData[i] = Modefl.lower;
\r
243 for (i = 0; i < pArraySize; i++)
\r
245 if (pData[i] > Modefl.upper)
\r
246 pData[i] = Modefl.upper;
\r
254 INTEGER SnarkRecFile::ReadRec(CHAR pRecName[81], CHAR pAlgName[4],
\r
255 unsigned INTEGER* pCount, unsigned INTEGER* pIter, REAL* pData)
\r
257 // reads the next reconstruction array
\r
259 const char* AlgName;
\r
260 const char* RecName;
\r
261 unsigned INTEGER NoOfRecs;
\r
263 // Reconstruction set
\r
264 if (SelectRecSet(RecSetNum) != 0)
\r
269 // Reconstruction set header
\r
270 if (GetRecSetName(&RecName) != 0)
\r
275 strncpy(pRecName, RecName, 80);
\r
279 // Reconstruction set header
\r
280 if (GetRecSetAlgName(&AlgName) != 0)
\r
287 strncat(pAlgName, AlgName, 4);
\r
289 // get number of recs in rec set
\r
290 if (GetNoOfRecs(&NoOfRecs))
\r
295 // select reconstruction within reconstruction set
\r
296 if (DIGFileSnarkRec::SelectRec(RecNum) != 0)
\r
302 if(GetRecCount(pCount) != 0) {
\r
306 *pCount = RecNum + 1;
\r
308 if (*pCount == NoOfRecs)
\r
314 *pIter = (*pCount - 1) % 50 + 2;
\r
317 if (GetRecData(DataBuffer) != 0)
\r
322 // flip image to Snark order
\r
323 FlipImage(pData, DataBuffer);
\r
325 unsigned int ArraySize;
\r
327 GetArrayNoOfItems(&ArraySize);
\r
329 ApplyLimits(ArraySize, pData);
\r
331 if (RecNum == (NoOfRecs - 1))
\r
332 { // if - end of recnstruction set
\r
337 { // not end of recnstruction set
\r
343 // closes the file
\r
344 INTEGER SnarkRecFile::Close()
\r
346 if (DataBuffer != NULL)
\r
347 delete[] DataBuffer;
\r
349 return DIGFile::Close();
\r
352 INTEGER SnarkRecFile::SelectRec(unsigned INTEGER pRecNo)
\r
354 // selects a reconstruction
\r
355 // input: array number in file
\r
356 // output: will set the array set number and the array number
\r
357 // first array is 0
\r
359 unsigned INTEGER NoOfRecSets;
\r
360 unsigned INTEGER NoOfRecs;
\r
361 unsigned INTEGER RecSetNo;
\r
362 unsigned INTEGER RecNo;
\r
364 if (GetNoOfRecSets(&NoOfRecSets) != 0)
\r
366 return -1; // error
\r
371 for (RecSetNo = 0; RecSetNo < NoOfRecSets; RecSetNo++)
\r
374 // select reconstruction set
\r
375 if (SelectRecSet(RecSetNo) != 0)
\r
377 return -1; // error
\r
380 // get number of reconstructions in set
\r
381 if (GetNoOfRecs(&NoOfRecs) != 0)
\r
383 return -1; // error
\r
386 // Check if reconstruction in current set
\r
387 if (RecNo < NoOfRecs)
\r
388 { // reconstruction in curent reconstrction set
\r
390 RecSetNum = RecSetNo;
\r
393 // select reconstruction within reconstruction set
\r
394 if (DIGFileSnarkRec::SelectRec(RecNum) != 0)
\r
401 RecNum -= NoOfRecs;
\r
404 return -1; // requested reconstruction number is too high
\r