TPCCLIB
Loading...
Searching...
No Matches
imgposv.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//#include "libtpcimgp.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Calculate the volume percentage of voxels with positive (>0) value in",
25 "a static PET image file in ECAT 6.3 or 7.x, NIfTI-1, or Analyze 7.5 format.",
26 " ",
27 "Usage: @P [Options] imgfile [resfile]",
28 " ",
29 "Options:",
30 " -stdoptions", // List standard options like --help, -v, etc
31 " ",
32 "See also: imghist, imgthrs, imgmask, imgmax, imginteg, imgslim",
33 " ",
34 "Keywords: image, threshold, mask",
35 0};
36/*****************************************************************************/
37
38/*****************************************************************************/
39/* Turn on the globbing of the command line, since it is disabled by default in
40 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
41 In Unix&Linux wildcard command line processing is enabled by default. */
42/*
43#undef _CRT_glob
44#define _CRT_glob -1
45*/
46int _dowildcard = -1;
47/*****************************************************************************/
48
49/*****************************************************************************/
53int main(int argc, char **argv)
54{
55 int ai, help=0, version=0, verbose=1;
56 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX];
57 int ret;
58 char *cptr;
59
60
61 /*
62 * Get arguments
63 */
64 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
65 imgfile[0]=outfile[0]=(char)0;
66 /* Options */
67 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
68 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
69 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
70 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
71 return(1);
72 } else break;
73
74 /* Print help or version? */
75 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
76 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
77 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
78
79 /* Process other arguments, starting from the first non-option */
80 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
81 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
82 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
83
84 /* Is something missing or wrong? */
85 if(!imgfile[0]) {
86 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
87 return(1);
88 }
89
90 /* In verbose mode print arguments and options */
91 if(verbose>1) {
92 printf("imgfile := %s\n", imgfile);
93 if(outfile[0]) printf("outfile := %s\n", outfile);
94 }
95
96
97 /*
98 * Read dynamic image
99 */
100 if(verbose>0) printf("reading static image %s\n", imgfile);
101 IMG img; imgInit(&img);
102 ret=imgRead(imgfile, &img);
103 if(ret) {
104 fprintf(stderr, "Error: %s\n", img.statmsg);
105 if(verbose>1) printf("ret := %d\n", ret);
106 return(2);
107 }
108 if(img.dimt!=1) {
109 fprintf(stderr, "Error: not a static image.\n");
110 imgEmpty(&img); return(2);
111 }
112 if(verbose>1) {
113 printf("dimx := %d\n", img.dimx);
114 printf("dimy := %d\n", img.dimy);
115 printf("dimz := %d\n", img.dimz);
116 }
117 if(imgNaNs(&img, 1)>0)
118 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
119
120
121 /*
122 * Check each pixel value
123 */
124 if(verbose>0) printf("counting positive voxels...\n");
125 long long total_n=0, posit_n=0;
126 int zi, yi, xi;
127 for(zi=0; zi<img.dimz; zi++)
128 for(yi=0; yi<img.dimy; yi++)
129 for(xi=0; xi<img.dimx; xi++) {
130 total_n++;
131 if(img.m[zi][yi][xi][0]>0.0) posit_n++;
132 }
133 double volper=100.0*(double)posit_n/(double)total_n;
134 imgEmpty(&img);
135
136 /* Report results */
137 if(!outfile[0] || verbose>0) {
138 printf("positive_volume_percentage := %g\n", volper);
139 }
140 if(!outfile[0]) return(0);
141 /* Save results */
142 char tmp[64]; sprintf(tmp, "%g", volper);
143 IFT ift; iftInit(&ift);
144 ret=iftPut(&ift, "positive_volume_percentage", tmp, NULL, 0);
145 if(ret==0) ret=iftWrite(&ift, outfile, 0);
146 iftEmpty(&ift);
147 if(ret) {
148 fprintf(stderr, "Error: cannot save the result.\n");
149 return(11);
150 }
151 if(verbose>0) printf("done.\n");
152
153 return(0);
154}
155/*****************************************************************************/
156
157/*****************************************************************************/
int iftPut(IFT *ift, char *key, char *value, char *cmt_type, int verbose)
Definition ift.c:82
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftWrite(IFT *ift, char *filename, int verbose)
Definition iftfile.c:282
unsigned long long imgNaNs(IMG *img, int fix)
Definition img.c:658
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.
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
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg