8#include "tpcclibConfig.h"
27 char *dbname,
char *ecatfile,
int flipping,
int scanner_type,
float zoom,
33static char *info[] = {
34 "Conversion of Analyze 7.5 database(s) to ECAT 7 image volume or ECAT 6.3",
37 "Usage: @P [Options] database",
41 " Images are written in ECAT 7 (default) or 6.3 format.",
43 " Data directory for ECAT images; by default current working directory.",
45 " Override the default and environment variable ANALYZE_FLIP setting",
46 " by always flipping/not flipping image in z-direction (planes).",
47 " If environment variable is not set, then default is y.",
48 " Images are always flipped in x,y-directions.",
51 "Analyze database can be specified as a path, or filename without extension.",
52 "Program reads time frame information from SIF, if SIF is located in",
53 "the Analyze database directory and if file is named with *.sif extension.",
56 "Conversion of all Analyze files in directory S:\\temp\\neuro to ECAT 7",
57 "images in directory C:\\data in PC/Windows:",
60 " @P s:\\temp\\neuro",
62 "Specific extensions to Analyze 7.5 format:",
63 " -Scale factor to retain quantitation in image_dimension.funused1",
64 " -Isotope halflife (sec) in image_dimension.funused3;",
65 " this does not imply whether data is corrected for decay or not.",
66 " -String in data_history.descrip tells whether data is corrected for decay;",
67 " 'Decay corrected.' or 'No decay correction.'",
69 "See also: ecat2ana, eframe, sif2ecat, e7emhdr, e7evhdr, flat2img, nii2ecat",
71 "Keywords: image, format conversion, ECAT, Analyze",
90int main(
int argc,
char **argv)
92 int ai, help=0, version=0, verbose=1;
93 int ret, n, errorNr=0, flipping, anaNr=0;
94 int scanner_type=0, outFormat=7;
96 char ecatdir[FILENAME_MAX], dbdir[FILENAME_MAX], ecatfile[FILENAME_MAX];
97 char temp[FILENAME_MAX], dbname[FILENAME_MAX], *cptr;
105 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
106 ecatdir[0]=dbdir[0]=ecatfile[0]=(char)0;
109 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
110 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
112 if(strncasecmp(cptr,
"FLIP=", 5)==0) {
113 cptr+=5;
if(*cptr==
'n' || *cptr==
'N') {flipping=0;
continue;}
114 else if(*cptr==
'y' || *cptr==
'Y') {flipping=1;
continue;}
115 }
else if(strncasecmp(cptr,
"ZOOM=", 5)==0) {
116 cptr+=5; zoom=
atof_dpi(cptr);
if(zoom>0.0)
continue;
117 }
else if(strncmp(cptr,
"7", 1)==0) {
118 outFormat=7;
continue;
119 }
else if(strncmp(cptr,
"6", 1)==0) {
120 outFormat=6;
continue;
121 }
else if(strncasecmp(cptr,
"SCANNER=", 8)==0) {
125 if(strcasecmp(cptr,
"HRRT")==0) {
127 fprintf(stderr,
"Warning: HRRT not fully supported.\n");
130 if(strncasecmp(cptr,
"HR", 2)==0) {scanner_type=
SCANNER_HRPLUS;
continue;}
131 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
133 if(strlen(cptr)>0) strcpy(ecatdir, cptr);
else strcpy(ecatdir,
".");
136 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
142 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
147 for(; ai<argc; ai++) {
149 strcpy(dbdir, argv[ai]);
continue;
151 fprintf(stderr,
"Error: invalid argument '%s'\n", argv[ai]);
157 fprintf(stderr,
"Error: Analyze database was not given.");
161 if(!ecatdir[0]) strcpy(ecatdir,
".");
165 printf(
"flipping := %d\n", flipping);
166 printf(
"zoom := %g\n", zoom);
167 printf(
"scanner_type := %d\n", scanner_type);
168 printf(
"outFormat := %d\n", outFormat);
169 printf(
"ecatdir := %s\n", ecatdir);
173 if(flipping==0) printf(
"image(s) will not be flipped in z-direction.\n");
174 else printf(
"image(s) will be flipped in z-direction.\n");
182 if(access(ecatdir, 0) == -1) {
183 if(verbose>0) fprintf(stdout,
"Creating subdirectory %s\n", ecatdir);
187 ret=mkdir(ecatdir, 00775);
190 fprintf(stderr,
"Error: cannot created subdirectory.\n");
194 }
else strcpy(ecatdir,
".");
207 fprintf(stderr,
"Error: cannot open Analyze image or directory %s\n",
212 ret=
niftiExists(dbname, NULL, NULL, NULL, NULL, 0, NULL);
214 fprintf(stderr,
"Error: %s contains NIfTI, not Analyze image\n", dbdir);
222 cptr=strrchr(dbname,
'/');
if(cptr==NULL) cptr=strrchr(dbname,
'\\');
223 if(cptr!=NULL) cptr++;
else cptr=dbname;
225 snprintf(ecatfile, FILENAME_MAX,
"%s\\%s", ecatdir, cptr);
227 snprintf(ecatfile, FILENAME_MAX,
"%s/%s", ecatdir, cptr);
229 if(outFormat==6) strcat(ecatfile,
".img");
else strcat(ecatfile,
".v");
230 if(verbose>2) printf(
" ECAT filename: '%s'\n", ecatfile);
235 fprintf(stderr,
" Error: %s would be overwritten.\n", ecatfile);
236 fprintf(stdout,
" No conversion is done for %s\n", dbname);
237 fflush(stdout); fflush(stderr);
238 errorNr++;
return(4);
241 if(access(ecatfile, 0)!=-1 && remove(ecatfile)!=0) {
242 fprintf(stderr,
"Error: cannot overwrite %s\n", ecatfile);
243 errorNr++;
return(5);
250 if(verbose>0) fprintf(stdout,
"%s :\n", dbname);
252 if(verbose>0) printf(
" processing Analyze image\n");
256 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
260 fprintf(stdout,
" Image saved in ECAT format in %s\n", ecatfile);
267 while((de=readdir(dp))!=NULL) {
268 if(verbose>6) printf(
"'%s'\n", de->d_name);
271 if(dbdir[ret]==
'/' || dbdir[ret]==
'\\') dbdir[ret]=(char)0;
277 n=strlen(de->d_name);
278 if(n<5 || strcmp((de->d_name)+n-4,
".hdr")!=0)
continue;
280 strcpy(temp, de->d_name); temp[n-4]=(char)0;
282 snprintf(dbname, FILENAME_MAX,
"%s\\%s", dbdir, temp);
284 snprintf(dbname, FILENAME_MAX,
"%s/%s", dbdir, temp);
288 if(verbose>0) fprintf(stdout,
"%s :\n", dbname);
291 ret=
niftiExists(dbname, NULL, NULL, NULL, NULL, 0, NULL);
293 fprintf(stderr,
" Error: %s is NIfTI image\n", dbname);
301 snprintf(ecatfile, FILENAME_MAX,
"%s\\%s", ecatdir, temp);
303 snprintf(ecatfile, FILENAME_MAX,
"%s/%s", ecatdir, temp);
305 if(outFormat==6) strcat(ecatfile,
".img");
else strcat(ecatfile,
".v");
306 if(verbose>1) printf(
" ECAT filename: '%s'\n", ecatfile);
313 fprintf(stderr,
" Error: %s would be overwritten.\n", ecatfile);
314 fprintf(stdout,
" No conversion is done for %s\n", dbname);
315 fflush(stdout); fflush(stderr);
320 if(access(ecatfile, 0)!=-1 && remove(ecatfile)!=0) {
321 fprintf(stderr,
"Error: cannot overwrite %s\n", ecatfile);
328 if(verbose>0) printf(
" processing Analyze image\n");
332 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
336 fprintf(stdout,
" Image saved in ECAT format in %s\n", ecatfile);
341 fprintf(stderr,
"Error: no Analyze files were found in %s\n", dbdir);
370 int zi, fi, ret, file_format=0;
372 if(verbose>0) printf(
"\nimgAnalyzeToEcat(%s, %s)\n", dbname, ecatfile);
374 if(dbname==NULL || ecatfile==NULL)
return STATUS_FAULT;
379 if(ret!=STATUS_OK) {
imgEmpty(&img);
return ret;}
382 if(verbose>1) printf(
" frame %d\n", fi);
387 if(zoom>0.0) img.
zoom=zoom;
394 if(ret!=STATUS_OK)
break;
395 if(verbose>2) printf(
" frame written.\n");
401 if(verbose>0 && ret==STATUS_NOMATRIX) {
402 fprintf(stdout,
" %d frame(s) processed.\n", fi);
404 if(ret!=STATUS_OK && ret!=STATUS_NOMATRIX) {
405 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)