11#include "tpcclibConfig.h"
25static char *info[] = {
26 "Extract the pixel values in PET image to a binary flat file",
27 "as 4-byte (32-bit) floating point values, in this order of matrices:",
28 "All image planes of the first frame, then planes of the 2nd frame, and so on.",
29 "The current platforms byte order (little or big endian) is used.",
31 "Plane and frame number and matrix dimensions x and y are written on screen",
32 "or in specified matrix information file (MIF), if file name for it is given.",
33 "These numbers are needed when binary data is imported in another application.",
35 "Usage: @P [Options] imagefile flatfile [MIF]",
44 " -bins=<Nr of bins>",
45 " This application supports also 2D ECAT sinogram data. With this option",
46 " only specified nr of bins in the mid part of sinogram are saved,",
47 " leaving out bins from both sides.",
50 "See also: flat2img, ecat2ana, ecat2nii, imgunit, eframe, convend",
52 "Keywords: ECAT, image, sinogram, format conversion",
71int main(
int argc,
char **argv)
73 int ai, help=0, version=0, verbose=1;
76 char imgfile[FILENAME_MAX], datfile[FILENAME_MAX], miffile[FILENAME_MAX];
83 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
84 imgfile[0]=datfile[0]=miffile[0]=(char)0;
86 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
87 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
90 if(strncasecmp(cptr,
"BINS=", 5)==0) {
91 binNr=atoi(cptr+5);
if(binNr>0.0)
continue;
92 }
else if(strncasecmp(cptr,
"INF=", 4)==0) {
93 strcpy(miffile, cptr+4);
if(strlen(miffile)>0)
continue;
94 }
else if(strncasecmp(cptr,
"MIF=", 4)==0) {
95 strcpy(miffile, cptr+4);
if(strlen(miffile)>0)
continue;
97 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
102 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
107 if(ai<argc) {
strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
108 if(ai<argc) {
strlcpy(datfile, argv[ai], FILENAME_MAX); ai++;}
109 if(ai<argc) {
strlcpy(miffile, argv[ai], FILENAME_MAX); ai++;}
110 if(ai<argc) {fprintf(stderr,
"Error: too many arguments.\n");
return(1);}
115 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
118 if(strcasecmp(imgfile, datfile)==0 || strcasecmp(imgfile, miffile)==0) {
119 fprintf(stderr,
"Error: same name for input and output file.\n");
125 printf(
"imgfile := %s\n", imgfile);
126 printf(
"datfile := %s\n", datfile);
127 printf(
"miffile := %s\n", miffile);
128 printf(
"binNr := %d\n", binNr);
132 if(verbose>1) printf(
"reading %s frame-by-frame\n", imgfile);
136 float *fdata=NULL, *fptr;
139 if(verbose>0) fprintf(stdout,
" processing frames");
140 while((ret=
imgReadFrame(imgfile, frameNr+1, &img, 0)) == 0) {
142 if(verbose>0) {fprintf(stdout,
"."); fflush(stdout);}
146 fprintf(stderr,
"\nError: bin nr can be used only with sinograms.\n");
149 if(binNr>0 && img.
dimx<=binNr) {
150 fprintf(stderr,
"\nError: bin nr should be smaller than sinogram dimx.\n");
153 if(binNr<1) binNr=img.
dimx;
159 if(verbose>1) printf(
"\nOpening output datafile %s\n", datfile);
160 if((fp=fopen(datfile,
"wb")) == NULL) {
161 fprintf(stderr,
"\nError: cannot open file %s\n", datfile);
165 pxlNr=binNr*img.
dimy;
166 fdata=(
float*)malloc(pxlNr*
sizeof(
float));
168 fprintf(stderr,
"\nError: out of memory\n");
169 imgEmpty(&img); fclose(fp);
return(5);
173 for(pi=0; pi<img.
dimz; pi++) {
175 for(yi=0; yi<img.
dimy; yi++)
176 for(xi=(img.
dimx-binNr)/2; xi<(img.
dimx+binNr)/2; xi++)
177 *fptr++=img.
m[pi][yi][xi][0];
179 if(fwrite((
float*)fdata, 4, pxlNr, fp) != pxlNr) {
180 fprintf(stderr,
"\nError: cannot write matrix data in %s\n", datfile);
181 fclose(fp); remove(datfile); free(fdata);
imgEmpty(&img);
189 if(ret==STATUS_NOMATRIX) fprintf(stdout,
" ok");
190 fprintf(stdout,
"\n");
192 if(fp!=NULL) fclose(fp);
194 if(verbose>3) printf(
"imgReadFrame() return value := %d\n", ret);
195 if(ret!=0 && ret!=STATUS_NOMATRIX) {
196 fprintf(stderr,
"Error: %s\n",
imgStatus(ret));
201 fprintf(stderr,
"Error: no frames could be read in %s\n", imgfile);
209 if(verbose>0 || !miffile[0]) {
211 fprintf(fp,
"%d %d %d %d\n", img.
dimz, frameNr, binNr, img.
dimy);
218 if(verbose>1) printf(
"Opening matrix information file %s\n", miffile);
219 if((fp=fopen(miffile,
"w")) == NULL) {
220 fprintf(stderr,
"Error: cannot open file %s\n", miffile);
223 ret=fprintf(fp,
"%d %d %d %d\n", img.
dimz, frameNr, binNr, img.
dimy);
226 fprintf(stderr,
"Error: cannot write in file %s\n", miffile);
229 if(verbose>0) fprintf(stdout,
"Matrix information saved in %s\n", miffile);
234 fprintf(stdout,
"written data as 4-byte floats in %s\n", datfile);
char * imgStatus(int status_index)
void imgEmpty(IMG *image)
int imgReadFrame(const char *fname, int frame_to_read, IMG *img, int frame_index)
Header file for libtpcimgio.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
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)