8#include "tpcclibConfig.h"
27static char *info[] = {
28 "Converts ECAT 6.3 images or ECAT 7 image volumes to Analyze 7.5 image format.",
30 "The resulting Analyze database consists of two files, Analyze image (*.img)",
31 "and header file (*.hdr).",
32 "Analyze image format does not contain information on the frame times.",
33 "Frame times can be retrieved from SIF file, which can be created optionally.",
34 "SIF can also be created using other software.",
36 "By default, data is saved in big endian byte order (Sun Sparc).",
38 "Usage: @P [Options] ecatfile(s)",
41 " -Little or -pc or -intel",
42 " Data is saved in little endian (PC Intel) byte order.",
43 " -Big or -sun or -sparc",
44 " Data is saved in big endian (Sun Sparc, Motorola, PowerPC) byte order",
47 " Data directory for Analyze files; by default the output directory",
48 " ana_unix_files or ana_pc_files is created under input directory.",
50 " Override the default and environment variable ANALYZE_FLIP setting",
51 " by always flipping/not flipping image in z-direction (planes).",
52 " If environment variable is not set, then default is y.",
53 " Images are always flipped in x,y-directions.",
55 " SIF is saved with Analyze files; note that existing SIF will be",
58 " Time frames are saved as separate Analyze files.",
64 "Specific extensions to Analyze 7.5 format:",
65 " -Scale factor to retain quantitation in image_dimension.funused1",
66 " -Isotope halflife (sec) in image_dimension.funused3;",
67 " this does not imply whether data is corrected for decay or not.",
68 " -String in data_history.descrip tells whether data is corrected for decay;",
69 " 'Decay corrected.' or 'No decay correction.'",
71 "See also: ana2ecat, anabyteo, ana_lhdr, eframe, img2flat, ecat2nii",
73 "Keywords: image, format conversion, ECAT, Analyze",
92int main(
int argc,
char **argv)
94 int ai, help=0, version=0, verbose=1;
95 int ret, errorNr=0, flipping, fi=0, ffi=0, fileNr=0;
97 int separate_frames=0;
100 char ecatfile[FILENAME_MAX], dbname[FILENAME_MAX], *cptr;
101 char temp[FILENAME_MAX], outputdir[FILENAME_MAX], dbdir[FILENAME_MAX];
110 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
111 outputdir[0]=ecatfile[0]=dbname[0]=dbdir[0]=(char)0;
115 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
116 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
118 if(strncasecmp(cptr,
"FLIP=", 5)==0) {
119 cptr+=5;
if(*cptr==
'n' || *cptr==
'N') {flipping=0;
continue;}
120 else if(*cptr==
'y' || *cptr==
'Y') {flipping=1;
continue;}
121 }
else if(strncasecmp(cptr,
"FRAMES", 2)==0) {
122 separate_frames=1;
continue;
123 }
else if(strcasecmp(cptr,
"LITTLE")==0) {
124 ana_order=0;
continue;
125 }
else if(strcasecmp(cptr,
"PC")==0) {
126 ana_order=0;
continue;
127 }
else if(strcasecmp(cptr,
"INTEL")==0) {
128 ana_order=0;
continue;
129 }
else if(strcasecmp(cptr,
"BIG")==0) {
130 ana_order=1;
continue;
131 }
else if(strcasecmp(cptr,
"SUN")==0) {
132 ana_order=1;
continue;
133 }
else if(strcasecmp(cptr,
"SPARC")==0) {
134 ana_order=1;
continue;
135 }
else if(strncasecmp(cptr,
"O=", 2)==0) {
137 if(strlen(cptr)>0) strcpy(outputdir, cptr);
else strcpy(outputdir,
".");
139 }
else if(strcasecmp(cptr,
"SIF")==0) {
140 save_sif=1;
continue;
142 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
147 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
152 for(; ai<argc; ai++) {
154 if(access(argv[ai], 0) == -1) {
155 fprintf(stderr,
"Error: file %s does not exist\n", argv[ai]);
164 fprintf(stderr,
"Error: no ECAT files were specified.\n");
171 printf(
"flipping := %d\n", flipping);
172 printf(
"ana_order := %d\n", ana_order);
173 printf(
"separate_frames := %d\n", separate_frames);
174 printf(
"save_sif := %d\n", save_sif);
175 printf(
"outputdir := %s\n", outputdir);
176 printf(
"fileNr := %d\n", fileNr);
180 if(flipping==0) printf(
"image(s) will not be flipped in z-direction.\n");
181 else printf(
"image(s) will be flipped in z-direction.\n");
188 if(verbose>1) printf(
"processing...\n");
190 for(ai=ffi; ai<argc; ai++) {
192 strcpy(ecatfile, argv[ai]);
193 if(verbose>0) {fprintf(stdout,
"%s : \n", ecatfile); fflush(stdout);}
201 if(strlen(outputdir)<1) {
203 strcpy(dbdir, ecatfile);
204 cptr=strrchr(dbdir,
'/');
if(cptr==NULL) cptr=strrchr(dbdir,
'\\');
205 if(cptr!=NULL) {cptr++; cptr[0]=(char)0;}
else dbdir[0]=(char)0;
207 if(ana_order==1) strcat(dbdir,
"ana_unix_files");
208 else strcat(dbdir,
"ana_pc_files");
210 strcpy(dbdir, outputdir);
213 if(dbdir[ret]==
'/' || dbdir[ret]==
'\\') dbdir[ret]=(char)0;
216 if(access(dbdir, 0) == -1) {
218 fprintf(stdout,
" Creating subdirectory %s\n", dbdir);
222 ret=mkdir(dbdir, 00775);
223#elif defined HAVE_MKDIR1
225#elif defined HAVE__MKDIR
231 fprintf(stderr,
" Error: cannot create subdirectory.\n");
232 fflush(stderr); errorNr++;
continue;
236 cptr=strrchr(ecatfile,
'/');
if(cptr==NULL) cptr=strrchr(ecatfile,
'\\');
237 if(cptr==NULL) cptr=ecatfile;
else cptr++;
239 snprintf(dbname, FILENAME_MAX,
"%s\\%s", dbdir, cptr);
241 snprintf(dbname, FILENAME_MAX,
"%s/%s", dbdir, cptr);
243 cptr=strrchr(dbname,
'.');
if(cptr!=NULL) cptr[0]=(char)0;
244 if(verbose>1) printf(
" Analyze db_name: '%s'\n", dbname);
247 if(separate_frames!=1 &&
anaExists(dbname)==0) {
248 strcpy(temp, dbname); strcat(temp,
".img");
249 if(access(temp, 0) != -1) {
250 fprintf(stderr,
" Error: %s would be overwritten.\n", temp);
251 fprintf(stdout,
" No conversion is done for %s\n", ecatfile);
252 fflush(stdout); fflush(stderr);
260 ret=
anaRemove(dbname);
if(ret)
return STATUS_CANNOTERASE;
266 if(verbose>1) fprintf(stdout,
" searching min and max in %s\n", ecatfile);
269 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
271 errorNr++; fflush(stderr);
continue;
274 printf(
" global_min := %g\n global_max := %g\n", fmin, fmax);
281 if(verbose>1) printf(
" reading header information\n");
284 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
285 errorNr++; fflush(stderr);
continue;
289 fprintf(stderr,
"Error: out of memory.\n");
290 errorNr++; fflush(stderr);
continue;
299 if(verbose>0) fprintf(stdout,
" processing %s\n", ecatfile);
301 while((ret=
imgReadFrame(ecatfile, fi+1, &img, 0)) == 0) {
302 if(verbose>1) printf(
" frame %d\n", fi+1);
310 if(separate_frames!=1) {
314 snprintf(temp, FILENAME_MAX,
"%s_fr%03d", dbname, fi+1);
319 if(ret!=STATUS_OK)
break;
320 if(verbose>3) printf(
" frame written.\n");
338 if(ret==STATUS_NOMATRIX)
339 fprintf(stdout,
" %d frame(s) processed.\n", fi);
341 if(ret!=STATUS_OK && ret!=STATUS_NOMATRIX) {
342 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
343 errorNr++; fflush(stderr);
continue;
349 char temp[FILENAME_MAX+4];
350 sprintf(temp,
"%s.sif", dbname);
353 fprintf(stderr,
"Error: cannot write %s\n", temp);
354 errorNr++; fflush(stderr);
continue;
361 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]