Skip to content
Snippets Groups Projects
Commit eb3bc9fe authored by Matthew Webster's avatar Matthew Webster
Browse files

Some more fireproofing

parent 2ef2d203
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
// CCOPYRIGHT
#include "newimage/newimageall.h"
#include "fslio/fslio.h"
#include <iostream>
using namespace NEWIMAGE;
......@@ -11,7 +12,7 @@ void print_usage(const string& progname)
{
cout << endl;
cout << "Usage: fslcorrecthd <input> <output>" << endl;
cout << " Note the input file must be an uncompressed NIFTI or ANALYZE file" << endl;
cout << " Note that fslcorrecthd only works uncompressed NIFTI or ANALYZE files" << endl;
}
int main(int argc,char *argv[])
......@@ -35,39 +36,45 @@ int main(int argc,char *argv[])
//check nifti-libs output versus raw header info
int offset =(int) ( fslio->niftiptr->iname_offset - hdr->dime.vox_offset );
int minft=(int)MIN(fslio->niftiptr->iname_offset,hdr->dime.vox_offset);
cerr << "number of bytes wrong: " << offset << endl;
cerr << "start at byte location: " << minft << endl;
cout << "number of bytes wrong: " << offset << endl << "start at byte location: " << minft << endl;
if (offset==0) {
cout << "No byte correction needed, exiting." << endl;
return 0;
}
if (FslIsCompressedFileType(FslGetFileType(fslio))) {
cerr << "Error: fslcorrecthd requires uncompressed input" << endl;
exit(1);
return 1;
}
ifstream input_file;
ofstream output_file;
char *temp,*outputName;
FslGetHdrImgNames(argv[2],fslio,&temp,&outputName);
char byte[1];
input_file.open(argv[1],ios::in | ios :: binary);
output_file.open(argv[2],ofstream::out | ofstream::binary);
for(int i=1;i<=minft;i++)
output_file.open(outputName,ofstream::out | ofstream::binary);
for(int i=1;i<=minft;i++) //Write Header
{
input_file.read(byte,1);
if (input_file.eof()) break;
output_file.write(byte,1);
}
for(int i=1;i<=abs(offset) && offset>0;i++)
for(int i=1;i<=abs(offset) && offset>0;i++) //Pad if we have missing 4 bytes
{
byte[0]=0;
output_file.write(byte,1);
}
}
for(int i=1;i<=abs(offset) && offset<0;i++)
for(int i=1;i<=abs(offset) && offset<0;i++) //Read past bad extensions/junk
{
input_file.read(byte,1);
}
while(true)
while(true) //Copy the data
{
input_file.read(byte,1);
if (input_file.eof()) break;
......@@ -75,7 +82,12 @@ int main(int argc,char *argv[])
}
output_file.close();
input_file.close();
return 0;
system(("FSLOUTPUTTYPE=NIFTI; ${FSLDIR}/bin/fslmaths " + string(outputName)).c_str()); //To clean up header
free(temp);
free(outputName);
free(hdr);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment