TPCCLIB
Loading...
Searching...
No Matches
imgmlist.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <string.h>
14#include <unistd.h>
15#include <time.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18#include "libtpcimgio.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "List image matrices with their min, max, and mean pixel values.",
24 "Image file in ECAT 6.3 or 7.x, NIfTI-1, and Analyze 7.5 format is accepted.",
25 " ",
26 "Usage: @P [Options] imgfile",
27 " ",
28 "Options:",
29 " -xyz | -xy",
30 " List x,y,z image volumes (default), or x,y image planes.",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "See also: imgmax, imgmaxp, imghead, lmlist, img2flat",
34 " ",
35 "Keywords: image, max, min, matrixlist",
36 0};
37/*****************************************************************************/
38
39/*****************************************************************************/
40/* Turn on the globbing of the command line, since it is disabled by default in
41 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
42 In Unix&Linux wildcard command line processing is enabled by default. */
43/*
44#undef _CRT_glob
45#define _CRT_glob -1
46*/
47int _dowildcard = -1;
48/*****************************************************************************/
49
50/*****************************************************************************/
54int main(int argc, char **argv)
55{
56 int ai, help=0, version=0, verbose=1;
57 int ret;
58 char imgfile[FILENAME_MAX], *cptr=NULL;
59 int listVol=1;
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 imgfile[0]=(char)0;
67 /* Options */
68 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
69 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
70 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
71 if(strcasecmp(cptr, "XYZ")==0) {
72 listVol=1; continue;
73 } else if(strcasecmp(cptr, "XY")==0) {
74 listVol=0; continue;
75 }
76 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
77 return(1);
78 } else break;
79
80 /* Print help or version? */
81 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
82 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
83 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
84
85
86 /* Process other arguments, starting from the first non-option */
87 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
88 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
89
90 /* Did we get all the information that we need? */
91 if(!imgfile[0]) {
92 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
93 return(1);
94 }
95
96 /* In verbose mode print arguments and options */
97 if(verbose>1) {
98 printf("imgfile := %s\n", imgfile);
99 printf("listVol := %d\n", listVol);
100 }
101
102 if(verbose>2) IMG_TEST=ECAT63_TEST=ECAT7_TEST=verbose-2;
104
105
106 /*
107 * Read the image file
108 */
109 if(verbose>1) fprintf(stdout, "reading %s\n", imgfile);
110 IMG img; imgInit(&img);
111 ret=imgRead(imgfile, &img);
112 if(ret) {
113 fprintf(stderr, "Error: %s\n", img.statmsg);
114 if(verbose>1) printf("ret := %d\n", ret);
115 return(2);
116 }
117 /* Check if PET data is raw or image */
118 if(img.type!=IMG_TYPE_IMAGE) {
119 fprintf(stderr, "Error: %s is not an image.\n", imgfile);
120 imgEmpty(&img); return(2);
121 }
122 int dimt, dimz, dimy, dimx;
123 dimt=img.dimt; dimz=img.dimz; dimy=img.dimy; dimx=img.dimx;
124 if(verbose>1) fprintf(stdout, " dim[x,y,z,t] := %d, %d, %d, %d\n", dimx, dimy, dimz, dimt);
125
126
127 /*
128 * List the contents
129 */
130 if(listVol==1) {
131 fprintf(stdout, "Frame\tMinValue\tMaxValue\tMeanValue\tValidN\tInvalidN\n"); fflush(stdout);
132 for(int t=0; t<dimt; t++) {
133 float mmax=nan(""), mmin=nan(""), msum=0.0;
134 int n=0, in=0;
135 for(int z=0; z<dimz; z++)
136 for(int y=0; y<dimy; y++)
137 for(int x=0; x<dimx; x++) {
138 if(!isfinite(img.m[z][y][x][t])) {in++; continue;}
139 if(!isfinite(mmax)) mmax=img.m[z][y][x][t];
140 if(!isfinite(mmin)) mmin=img.m[z][y][x][t];
141 if(img.m[z][y][x][t]>mmax) mmax=img.m[z][y][x][t];
142 if(img.m[z][y][x][t]<mmin) mmin=img.m[z][y][x][t];
143 msum+=img.m[z][y][x][t];
144 n++;
145 }
146 fprintf(stdout, "%d\t%.3e\t%.3e\t%.3e\t%d\t%d\n", 1+t, mmin, mmax, msum/(float)n, n, in);
147 fflush(stdout);
148 }
149 } else {
150 fprintf(stdout, "Frame\tPlane\tMinValue\tMaxValue\tMeanValue\tValidN\nInvalidN\n"); fflush(stdout);
151 for(int t=0; t<dimt; t++)
152 for(int z=0; z<dimz; z++) {
153 float mmax=nan(""), mmin=nan(""), msum=0.0;
154 int n=0, in=0;
155 for(int y=0; y<dimy; y++)
156 for(int x=0; x<dimx; x++) {
157 if(!isfinite(img.m[z][y][x][t])) {
158 if(verbose>1) printf("pixel[%d][%d][%d][%d] := %g\n", z, y, x, t, img.m[z][y][x][t]);
159 in++; continue;
160 }
161 if(!isfinite(mmax)) mmax=img.m[z][y][x][t];
162 if(!isfinite(mmin)) mmin=img.m[z][y][x][t];
163 if(img.m[z][y][x][t]>mmax) mmax=img.m[z][y][x][t];
164 if(img.m[z][y][x][t]<mmin) mmin=img.m[z][y][x][t];
165 msum+=img.m[z][y][x][t];
166 n++;
167 }
168 fprintf(stdout, "%d\t%d\t%.3e\t%.3e\t%.3e\t%d\t%d\n", 1+t, 1+z, mmin, mmax, msum/(float)n, n, in);
169 fflush(stdout);
170 }
171 }
172
173 return(0);
174}
175/*****************************************************************************/
176
177/*****************************************************************************/
int ECAT63_TEST
Definition ecat63h.c:6
int ECAT7_TEST
Definition ecat7h.c:6
int IMG_TEST
Definition img.c:6
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
Header file for libtpcimgio.
#define IMG_TYPE_IMAGE
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
unsigned short int dimx
char type
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg