8#include "tpcclibConfig.h"
30 char *dbname,
char *ecatfile,
int flipping,
int scanner_type,
float zoom,
36static char *info[] = {
37 "Conversion of Analyze 7.5 database(s) to ECAT 7 image volume or ECAT 6.3",
40 "Usage: @P [Options] database",
44 " Images are written in ECAT 7 (default) or 6.3 format.",
46 " Data directory for ECAT images; by default current working directory.",
48 " Override the default and environment variable ANALYZE_FLIP setting",
49 " by always flipping/not flipping image in z-direction (planes).",
50 " If environment variable is not set, then default is y.",
51 " Images are always flipped in x,y-directions.",
54 "Analyze database can be specified as a path, or filename without extension.",
55 "Program reads time frame information from SIF, if SIF is located in",
56 "the Analyze database directory and if file is named with *.sif extension.",
59 "Conversion of all Analyze files in directory S:\\temp\\neuro to ECAT 7",
60 "images in directory C:\\data in PC/Windows:",
63 " @P s:\\temp\\neuro",
65 "Specific extensions to Analyze 7.5 format:",
66 " -Scale factor to retain quantitation in image_dimension.funused1",
67 " -Isotope halflife (sec) in image_dimension.funused3;",
68 " this does not imply whether data is corrected for decay or not.",
69 " -String in data_history.descrip tells whether data is corrected for decay;",
70 " 'Decay corrected.' or 'No decay correction.'",
72 "See also: ecat2ana, eframe, sif2ecat, e7emhdr, e7evhdr, flat2img, nii2ecat",
74 "Keywords: image, format conversion, ECAT, Analyze",
93int main(
int argc,
char **argv)
95 int ai, help=0, version=0, verbose=1;
96 int ret, n, errorNr=0, flipping, anaNr=0;
97 int scanner_type=0, outFormat=7;
99 char ecatdir[FILENAME_MAX], dbdir[FILENAME_MAX], ecatfile[FILENAME_MAX];
100 char temp[FILENAME_MAX], dbname[FILENAME_MAX], *cptr;
108 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
109 ecatdir[0]=dbdir[0]=ecatfile[0]=(char)0;
112 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
113 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
115 if(strncasecmp(cptr,
"FLIP=", 5)==0) {
116 cptr+=5;
if(*cptr==
'n' || *cptr==
'N') {flipping=0;
continue;}
117 else if(*cptr==
'y' || *cptr==
'Y') {flipping=1;
continue;}
118 }
else if(strncasecmp(cptr,
"ZOOM=", 5)==0) {
119 cptr+=5; zoom=
atof_dpi(cptr);
if(zoom>0.0)
continue;
120 }
else if(strncmp(cptr,
"7", 1)==0) {
121 outFormat=7;
continue;
122 }
else if(strncmp(cptr,
"6", 1)==0) {
123 outFormat=6;
continue;
124 }
else if(strncasecmp(cptr,
"SCANNER=", 8)==0) {
128 if(strcasecmp(cptr,
"HRRT")==0) {
130 fprintf(stderr,
"Warning: HRRT not fully supported.\n");
133 if(strncasecmp(cptr,
"HR", 2)==0) {scanner_type=
SCANNER_HRPLUS;
continue;}
134 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
136 if(strlen(cptr)>0) strcpy(ecatdir, cptr);
else strcpy(ecatdir,
".");
139 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
145 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
150 for(; ai<argc; ai++) {
152 strcpy(dbdir, argv[ai]);
continue;
154 fprintf(stderr,
"Error: invalid argument '%s'\n", argv[ai]);
160 fprintf(stderr,
"Error: Analyze database was not given.");
164 if(!ecatdir[0]) strcpy(ecatdir,
".");
168 printf(
"flipping := %d\n", flipping);
169 printf(
"zoom := %g\n", zoom);
170 printf(
"scanner_type := %d\n", scanner_type);
171 printf(
"outFormat := %d\n", outFormat);
172 printf(
"ecatdir := %s\n", ecatdir);
176 if(flipping==0) printf(
"image(s) will not be flipped in z-direction.\n");
177 else printf(
"image(s) will be flipped in z-direction.\n");
185 if(access(ecatdir, 0) == -1) {
186 if(verbose>0) fprintf(stdout,
"Creating subdirectory %s\n", ecatdir);
188 ret=mkdir(ecatdir, 00775);
189#elif defined HAVE_MKDIR1
191#elif defined HAVE__MKDIR
197 fprintf(stderr,
"Error: cannot created subdirectory.\n");
201 }
else strcpy(ecatdir,
".");
214 fprintf(stderr,
"Error: cannot open Analyze image or directory %s\n",
219 ret=
niftiExists(dbname, NULL, NULL, NULL, NULL, 0, NULL);
221 fprintf(stderr,
"Error: %s contains NIfTI, not Analyze image\n", dbdir);
229 cptr=strrchr(dbname,
'/');
if(cptr==NULL) cptr=strrchr(dbname,
'\\');
230 if(cptr!=NULL) cptr++;
else cptr=dbname;
232 snprintf(ecatfile, FILENAME_MAX,
"%s\\%s", ecatdir, cptr);
234 snprintf(ecatfile, FILENAME_MAX,
"%s/%s", ecatdir, cptr);
236 if(outFormat==6) strcat(ecatfile,
".img");
else strcat(ecatfile,
".v");
237 if(verbose>2) printf(
" ECAT filename: '%s'\n", ecatfile);
242 fprintf(stderr,
" Error: %s would be overwritten.\n", ecatfile);
243 fprintf(stdout,
" No conversion is done for %s\n", dbname);
244 fflush(stdout); fflush(stderr);
245 errorNr++;
return(4);
248 if(access(ecatfile, 0)!=-1 && remove(ecatfile)!=0) {
249 fprintf(stderr,
"Error: cannot overwrite %s\n", ecatfile);
250 errorNr++;
return(5);
257 if(verbose>0) fprintf(stdout,
"%s :\n", dbname);
259 if(verbose>0) printf(
" processing Analyze image\n");
263 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
267 fprintf(stdout,
" Image saved in ECAT format in %s\n", ecatfile);
274 while((de=readdir(dp))!=NULL) {
275 if(verbose>6) printf(
"'%s'\n", de->d_name);
278 if(dbdir[ret]==
'/' || dbdir[ret]==
'\\') dbdir[ret]=(char)0;
284 n=strlen(de->d_name);
285 if(n<5 || strcmp((de->d_name)+n-4,
".hdr")!=0)
continue;
287 strcpy(temp, de->d_name); temp[n-4]=(char)0;
289 snprintf(dbname, FILENAME_MAX,
"%s\\%s", dbdir, temp);
291 snprintf(dbname, FILENAME_MAX,
"%s/%s", dbdir, temp);
295 if(verbose>0) fprintf(stdout,
"%s :\n", dbname);
298 ret=
niftiExists(dbname, NULL, NULL, NULL, NULL, 0, NULL);
300 fprintf(stderr,
" Error: %s is NIfTI image\n", dbname);
308 snprintf(ecatfile, FILENAME_MAX,
"%s\\%s", ecatdir, temp);
310 snprintf(ecatfile, FILENAME_MAX,
"%s/%s", ecatdir, temp);
312 if(outFormat==6) strcat(ecatfile,
".img");
else strcat(ecatfile,
".v");
313 if(verbose>1) printf(
" ECAT filename: '%s'\n", ecatfile);
320 fprintf(stderr,
" Error: %s would be overwritten.\n", ecatfile);
321 fprintf(stdout,
" No conversion is done for %s\n", dbname);
322 fflush(stdout); fflush(stderr);
327 if(access(ecatfile, 0)!=-1 && remove(ecatfile)!=0) {
328 fprintf(stderr,
"Error: cannot overwrite %s\n", ecatfile);
335 if(verbose>0) printf(
" processing Analyze image\n");
339 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
343 fprintf(stdout,
" Image saved in ECAT format in %s\n", ecatfile);
348 fprintf(stderr,
"Error: no Analyze files were found in %s\n", dbdir);
377 int zi, fi, ret, file_format=0;
379 if(verbose>0) printf(
"\nimgAnalyzeToEcat(%s, %s)\n", dbname, ecatfile);
381 if(dbname==NULL || ecatfile==NULL)
return STATUS_FAULT;
386 if(ret!=STATUS_OK) {
imgEmpty(&img);
return ret;}
389 if(verbose>1) printf(
" frame %d\n", fi);
394 if(zoom>0.0) img.
zoom=zoom;
401 if(ret!=STATUS_OK)
break;
402 if(verbose>2) printf(
" frame written.\n");
408 if(verbose>0 && ret==STATUS_NOMATRIX) {
409 fprintf(stdout,
" %d frame(s) processed.\n", fi);
411 if(ret!=STATUS_OK && ret!=STATUS_NOMATRIX) {
412 remove(ecatfile);
return ret;
int imgAnalyzeToEcat(char *dbname, char *ecatfile, int flipping, int scanner_type, float zoom, int verbose)
void anaRemoveFNameExtension(char *fname)
int anaExists(const char *dbname)
double atof_dpi(char *str)
char * imgStatus(int status_index)
void imgEmpty(IMG *image)
int imgReadAnalyzeFirstFrame(const char *fname, IMG *img)
int imgReadAnalyzeFrame(const char *fname, int frame_to_read, IMG *img, int frame_index)
int imgWriteFrame(const char *fname, int frame_to_write, IMG *img, int frame_index)
void imgFlipPlanes(IMG *img)
int imgSetScanner(IMG *img, int scanner_type)
Header file for libtpcimgio.
int niftiExists(const char *dbname, char *hdrfile, char *imgile, char *siffile, NIFTI_DSR *header, int verbose, char *status)
Header file for libtpcimgp.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)