Skip to content
Snippets Groups Projects
Commit 9ed71a84 authored by Mark Jenkinson's avatar Mark Jenkinson
Browse files

New versions of avwcreatehd and other minor mods

parent 58e215d7
No related branches found
No related tags found
No related merge requests found
...@@ -84,8 +84,9 @@ int main(argc,argv) ...@@ -84,8 +84,9 @@ int main(argc,argv)
/* {{{ copy geom */ /* {{{ copy geom */
/* memcpy(hdr2.dime.vox_units,hdr.dime.vox_units,4); */ /* memcpy(hdr2.dime.vox_units,hdr.dime.vox_units,4); */
FslGetVoxUnits(src, thestring); /* Note that Get and Set VoxDim work exclusively in mm and sec now */
FslSetVoxUnits(dest, thestring); /* FslGetVoxUnits(src, thestring); */
/* FslSetVoxUnits(dest, thestring); */
/* for(i=0;i<8;i++) */ /* for(i=0;i<8;i++) */
/* { */ /* { */
......
...@@ -13,7 +13,7 @@ void usage(void) ...@@ -13,7 +13,7 @@ void usage(void)
printf("\nUsage: avwcreatehd <xsize> <ysize> <zsize> <tsize> <xvoxsize> <yvoxsize> <zvoxsize> <tr> <xorigin> <yorigin> <zorigin> <datatype> <headername> [-r]\n"); printf("\nUsage: avwcreatehd <xsize> <ysize> <zsize> <tsize> <xvoxsize> <yvoxsize> <zvoxsize> <tr> <xorigin> <yorigin> <zorigin> <datatype> <headername> [-r]\n");
printf(" avwcreatehd <nifti_xml_file> <headername> [-r]\n\n"); printf(" avwcreatehd <nifti_xml_file> <headername> [-r]\n\n");
printf(" In all forms, [-r] => reverse byte order (to match byte order in data)\n"); printf(" In all forms, [-r] => reverse byte order (to match byte order in data)\n");
printf(" In the second form, an XML form of nifti header is read (as output by avwhd -n)\n"); printf(" In the second form, an XML-ish form of nifti header is read (as output by avwhd -x)\n");
printf(" Note that stdin is used if '-' is used in place of a filename\n"); printf(" Note that stdin is used if '-' is used in place of a filename\n");
exit(1); exit(1);
} }
...@@ -22,19 +22,37 @@ void usage(void) ...@@ -22,19 +22,37 @@ void usage(void)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FSLIO* fslio; FSLIO* fslio;
char *buffer, *hdrxml; void *buffer=NULL;
char *hdrxml, *filename;
int fileread=1, filetype; int fileread=1, filetype;
long bufsize=0;
short x,y,z,v, dt;
if (argc<2) if (argc<2)
usage(); usage();
if ((argc==3) || (argc==4)) { if ((argc==3) || (argc==4)) {
/* use the XML form of header specification */ /* use the XML form of header specification */
fslio = FslOpen(argv[2],"wb"); filename = argv[2];
} else { } else {
fslio = FslOpen(argv[13],"wb"); filename = argv[13];
}
/* check if file already exists and if so, read the image contents */
/* also store the size of this buffer for later in case it is wrong */
if (FslFileExists(filename)) {
/* buffer = FslReadAllVolumes(fslio,filename); */
fslio = FslOpen(filename,"rb");
FslGetDim(fslio,&x,&y,&z,&v);
bufsize = x * y * z * v * FslGetDataType(fslio,&dt) / 8;
buffer = (void *) calloc(bufsize,1);
FslReadVolumes(fslio,buffer,v);
FslClose(fslio);
} }
fslio = FslOpen(filename,"wb");
filetype = FslGetFileType(fslio); filetype = FslGetFileType(fslio);
if (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_MINC) { if (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_MINC) {
...@@ -59,11 +77,13 @@ int main(int argc, char **argv) ...@@ -59,11 +77,13 @@ int main(int argc, char **argv)
} else { } else {
/* read XML form */ /* read XML form */
char *newstr; char *newstr, *oldfname, *oldiname;
FILE *fileptr=NULL; FILE *fileptr=NULL;
if (strcmp(argv[1],"-")==0) {fileread=0;} if (strcmp(argv[1],"-")==0) {fileread=0;}
newstr = (char *)calloc(1,10000); newstr = (char *)calloc(10000,1);
hdrxml = (char *)calloc(1,65534); /* too long, to be safe */ oldfname = (char *)calloc(10000,1);
oldiname = (char *)calloc(10000,1);
hdrxml = (char *)calloc(65534,1); /* too long, to be safe */
if (fileread) { if (fileread) {
fileptr = fopen(argv[1],"rb"); fileptr = fopen(argv[1],"rb");
...@@ -83,11 +103,19 @@ int main(int argc, char **argv) ...@@ -83,11 +103,19 @@ int main(int argc, char **argv)
strcat(hdrxml,newstr); strcat(hdrxml,newstr);
} while (strcmp(newstr + strlen(newstr) - 2,"/>")!=0); } while (strcmp(newstr + strlen(newstr) - 2,"/>")!=0);
strcpy(oldfname,fslio->niftiptr->fname);
strcpy(oldiname,fslio->niftiptr->iname);
fslio->niftiptr = nifti_image_from_ascii(hdrxml); fslio->niftiptr = nifti_image_from_ascii(hdrxml);
if (fslio->niftiptr == NULL) { if (fslio->niftiptr == NULL) {
fprintf(stderr,"Incomplete or incorrect text: could not form header info\n"); fprintf(stderr,"Incomplete or incorrect text: could not form header info\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fslio->niftiptr->fname = oldfname;
fslio->niftiptr->iname = oldiname;
free(hdrxml); free(hdrxml);
if (fileread) fclose(fileptr); if (fileread) fclose(fileptr);
} }
...@@ -108,9 +136,15 @@ int main(int argc, char **argv) ...@@ -108,9 +136,15 @@ int main(int argc, char **argv)
/* write header */ /* write header */
FslWriteHeader(fslio); FslWriteHeader(fslio);
/* make a zero image here */ /* if previously read buffer is wrong size then make a zero image here */
buffer = (char *) calloc(fslio->niftiptr->nvox,fslio->niftiptr->nbyper); FslGetDim(fslio,&x,&y,&z,&v);
if ( bufsize != ( x * y * z * v * FslGetDataType(fslio,&dt)/8) ) {
if (bufsize>0) free(buffer); /* only if previously read */
buffer = (void *) calloc(x * y * z * v,FslGetDataType(fslio,&dt)/8);
}
/* write the data out - either from previous read or zeros */
FslWriteVolumes(fslio,buffer,fslio->niftiptr->dim[4]); FslWriteVolumes(fslio,buffer,fslio->niftiptr->dim[4]);
FslClose(fslio); FslClose(fslio);
......
...@@ -39,7 +39,6 @@ int main(int argc, char **argv) ...@@ -39,7 +39,6 @@ int main(int argc, char **argv)
FslGetDim(src,&x,&y,&z,&v); FslGetDim(src,&x,&y,&z,&v);
FslGetVoxDim(src,&vx,&vy,&vz,&tr); FslGetVoxDim(src,&vx,&vy,&vz,&tr);
FslGetVoxUnits(src, units);
bufsz = x * y * z * (FslGetDataType(src, &t) / 8); bufsz = x * y * z * (FslGetDataType(src, &t) / 8);
buffer = (void *) calloc(bufsz, sizeof(char)); buffer = (void *) calloc(bufsz, sizeof(char));
......
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