8#include "tpcclibConfig.h"
24static char *info[] = {
25 "Converts ECAT 6.3 images or ECAT 7 image volumes to Analyze 7.5 image format.",
27 "The resulting Analyze database consists of two files, Analyze image (*.img)",
28 "and header file (*.hdr).",
29 "Analyze image format does not contain information on the frame times.",
30 "Frame times can be retrieved from SIF file, which can be created optionally.",
31 "SIF can also be created using other software.",
33 "By default, data is saved in big endian byte order (Sun Sparc).",
35 "Usage: @P [Options] ecatfile(s)",
38 " -Little or -pc or -intel",
39 " Data is saved in little endian (PC Intel) byte order.",
40 " -Big or -sun or -sparc",
41 " Data is saved in big endian (Sun Sparc, Motorola, PowerPC) byte order",
44 " Data directory for Analyze files; by default the output directory",
45 " ana_unix_files or ana_pc_files is created under input directory.",
47 " Override the default and environment variable ANALYZE_FLIP setting",
48 " by always flipping/not flipping image in z-direction (planes).",
49 " If environment variable is not set, then default is y.",
50 " Images are always flipped in x,y-directions.",
52 " SIF is saved with Analyze files; note that existing SIF will be",
55 " Time frames are saved as separate Analyze files.",
61 "Specific extensions to Analyze 7.5 format:",
62 " -Scale factor to retain quantitation in image_dimension.funused1",
63 " -Isotope halflife (sec) in image_dimension.funused3;",
64 " this does not imply whether data is corrected for decay or not.",
65 " -String in data_history.descrip tells whether data is corrected for decay;",
66 " 'Decay corrected.' or 'No decay correction.'",
68 "See also: ana2ecat, anabyteo, ana_lhdr, eframe, img2flat, ecat2nii",
70 "Keywords: image, format conversion, ECAT, Analyze",
89int main(
int argc,
char **argv)
91 int ai, help=0, version=0, verbose=1;
92 int ret, errorNr=0, flipping, fi=0, ffi=0, fileNr=0;
94 int separate_frames=0;
97 char ecatfile[FILENAME_MAX], dbname[FILENAME_MAX], *cptr;
98 char temp[FILENAME_MAX], outputdir[FILENAME_MAX], dbdir[FILENAME_MAX];
107 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
108 outputdir[0]=ecatfile[0]=dbname[0]=dbdir[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,
"FRAMES", 2)==0) {
119 separate_frames=1;
continue;
120 }
else if(strcasecmp(cptr,
"LITTLE")==0) {
121 ana_order=0;
continue;
122 }
else if(strcasecmp(cptr,
"PC")==0) {
123 ana_order=0;
continue;
124 }
else if(strcasecmp(cptr,
"INTEL")==0) {
125 ana_order=0;
continue;
126 }
else if(strcasecmp(cptr,
"BIG")==0) {
127 ana_order=1;
continue;
128 }
else if(strcasecmp(cptr,
"SUN")==0) {
129 ana_order=1;
continue;
130 }
else if(strcasecmp(cptr,
"SPARC")==0) {
131 ana_order=1;
continue;
132 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
134 if(strlen(cptr)>0) strcpy(outputdir, cptr);
else strcpy(outputdir,
".");
136 }
else if(strcasecmp(cptr,
"SIF")==0) {
137 save_sif=1;
continue;
139 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
144 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
149 for(; ai<argc; ai++) {
151 if(access(argv[ai], 0) == -1) {
152 fprintf(stderr,
"Error: file %s does not exist\n", argv[ai]);
161 fprintf(stderr,
"Error: no ECAT files were specified.\n");
168 printf(
"flipping := %d\n", flipping);
169 printf(
"ana_order := %d\n", ana_order);
170 printf(
"separate_frames := %d\n", separate_frames);
171 printf(
"save_sif := %d\n", save_sif);
172 printf(
"outputdir := %s\n", outputdir);
173 printf(
"fileNr := %d\n", fileNr);
177 if(flipping==0) printf(
"image(s) will not be flipped in z-direction.\n");
178 else printf(
"image(s) will be flipped in z-direction.\n");
185 if(verbose>1) printf(
"processing...\n");
187 for(ai=ffi; ai<argc; ai++) {
189 strcpy(ecatfile, argv[ai]);
190 if(verbose>0) {fprintf(stdout,
"%s : \n", ecatfile); fflush(stdout);}
198 if(strlen(outputdir)<1) {
200 strcpy(dbdir, ecatfile);
201 cptr=strrchr(dbdir,
'/');
if(cptr==NULL) cptr=strrchr(dbdir,
'\\');
202 if(cptr!=NULL) {cptr++; cptr[0]=(char)0;}
else dbdir[0]=(char)0;
204 if(ana_order==1) strcat(dbdir,
"ana_unix_files");
205 else strcat(dbdir,
"ana_pc_files");
207 strcpy(dbdir, outputdir);
210 if(dbdir[ret]==
'/' || dbdir[ret]==
'\\') dbdir[ret]=(char)0;
213 if(access(dbdir, 0) == -1) {
215 fprintf(stdout,
" Creating subdirectory %s\n", dbdir);
221 ret=mkdir(dbdir, 00775);
224 fprintf(stderr,
" Error: cannot create subdirectory.\n");
225 fflush(stderr); errorNr++;
continue;
229 cptr=strrchr(ecatfile,
'/');
if(cptr==NULL) cptr=strrchr(ecatfile,
'\\');
230 if(cptr==NULL) cptr=ecatfile;
else cptr++;
232 snprintf(dbname, FILENAME_MAX,
"%s\\%s", dbdir, cptr);
234 snprintf(dbname, FILENAME_MAX,
"%s/%s", dbdir, cptr);
236 cptr=strrchr(dbname,
'.');
if(cptr!=NULL) cptr[0]=(char)0;
237 if(verbose>1) printf(
" Analyze db_name: '%s'\n", dbname);
240 if(separate_frames!=1 &&
anaExists(dbname)==0) {
241 strcpy(temp, dbname); strcat(temp,
".img");
242 if(access(temp, 0) != -1) {
243 fprintf(stderr,
" Error: %s would be overwritten.\n", temp);
244 fprintf(stdout,
" No conversion is done for %s\n", ecatfile);
245 fflush(stdout); fflush(stderr);
253 ret=
anaRemove(dbname);
if(ret)
return STATUS_CANNOTERASE;
259 if(verbose>1) fprintf(stdout,
" searching min and max in %s\n", ecatfile);
262 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
264 errorNr++; fflush(stderr);
continue;
267 printf(
" global_min := %g\n global_max := %g\n", fmin, fmax);
274 if(verbose>1) printf(
" reading header information\n");
277 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
278 errorNr++; fflush(stderr);
continue;
282 fprintf(stderr,
"Error: out of memory.\n");
283 errorNr++; fflush(stderr);
continue;
292 if(verbose>0) fprintf(stdout,
" processing %s\n", ecatfile);
294 while((ret=
imgReadFrame(ecatfile, fi+1, &img, 0)) == 0) {
295 if(verbose>1) printf(
" frame %d\n", fi+1);
303 if(separate_frames!=1) {
307 snprintf(temp, FILENAME_MAX,
"%s_fr%03d", dbname, fi+1);
312 if(ret!=STATUS_OK)
break;
313 if(verbose>3) printf(
" frame written.\n");
331 if(ret==STATUS_NOMATRIX)
332 fprintf(stdout,
" %d frame(s) processed.\n", fi);
334 if(ret!=STATUS_OK && ret!=STATUS_NOMATRIX) {
335 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
336 errorNr++; fflush(stderr);
continue;
342 char temp[FILENAME_MAX+4];
343 sprintf(temp,
"%s.sif", dbname);
346 fprintf(stderr,
"Error: cannot write %s\n", temp);
347 errorNr++; fflush(stderr);
continue;
354 if(errorNr>0)
return(errorNr+10);
int anaExists(const char *dbname)
int anaRemove(const char *dbname)
char * imgStatus(int status_index)
void imgEmpty(IMG *image)
int imgWriteAnalyzeFrame(const char *dbname, int frame_to_write, IMG *img, int frame_index, float fmin, float fmax)
char * imgIsotope(IMG *img)
int imgReadFrame(const char *fname, int frame_to_read, IMG *img, int frame_index)
int imgReadHeader(const char *fname, IMG *img, int format)
void imgFlipPlanes(IMG *img)
int imgReadMinMax(const char *fname, float *fmin, float *fmax)
int imgMinMax(IMG *img, float *minvalue, float *maxvalue)
Header file for libtpcimgio.
int sifWrite(SIF *data, char *filename)
int sifSetmem(SIF *data, int frameNr)
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)
char studyNr[MAX_STUDYNR_LEN+1]
char studynr[MAX_STUDYNR_LEN+1]